473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Registry Editing: NullReferenceException driving me crazy. Please help me out...



Hello,

I'm absolutely new to programming, so please don't be too hard on me...

I'm trying to make a simple form application to hide or show hidden
files. When I try to run the application I get a
"System.NullReferenceException: Object reference not set to an instance
of an object."
I found many topics related to this problem, but I still cannot figure
it out.

Here's the code:

partial class Form1 : Form

{
public Form1()
{
InitializeComponent();
}

private void HideFiles()
{
RegistryKey MyKey = Registry.CurrentUser;
MyKey =
MyKey.OpenSubKey(".Software\\Microsoft\\Windows\\C urrentVersion\\Explore
r\\Advanced", true);
MyKey.SetValue("Hidden",2);
MyKey.Close();
}

private void ShowFiles()
{
RegistryKey MyKey;
MyKey = Registry.CurrentUser;
MyKey =
MyKey.OpenSubKey(".Software\\Microsoft\\Windows\\C urrentVersion\\Explore
r\\Advanced", true);
MyKey.SetValue("Hidden",1);
MyKey.Close();
}

private void ShowButton_Click(object sender, EventArgs e)
{
ShowFiles();
}

private void HideButton_Click(object sender, EventArgs e)
{
HideFiles();
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
2 2114
Hi Markali,

Don't worry, we all have experienced this error and do so still :)
The error means you have an reference that is null and you try to do
something with it.

I suspect it is the ...

RegistryKey MyKey

.... part. OpenSubKey returns null when it fails to aquire the subkey, so
when you try to do ...

MyKey.SetValue("Hidden",2);
MyKey.Close();

.... it will choke.

put something like this in the method

if(MyKey != null)
{
MyKey.SetValue("Hidden",2);
MyKey.Close();
}

On Sat, 05 Mar 2005 03:12:35 -0800, Markali A. <ya****@hotmail.com> wrote:


Hello,

I'm absolutely new to programming, so please don't be too hard on me...

I'm trying to make a simple form application to hide or show hidden
files. When I try to run the application I get a
"System.NullReferenceException: Object reference not set to an instance
of an object."
I found many topics related to this problem, but I still cannot figure
it out.

Here's the code:

partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void HideFiles()
{
RegistryKey MyKey = Registry.CurrentUser;
MyKey =
MyKey.OpenSubKey(".Software\\Microsoft\\Windows\\C urrentVersion\\Explore
r\\Advanced", true);
MyKey.SetValue("Hidden",2);
MyKey.Close();
}

private void ShowFiles()
{
RegistryKey MyKey;
MyKey = Registry.CurrentUser;
MyKey =
MyKey.OpenSubKey(".Software\\Microsoft\\Windows\\C urrentVersion\\Explore
r\\Advanced", true);
MyKey.SetValue("Hidden",1);
MyKey.Close();
}

private void ShowButton_Click(object sender, EventArgs e)
{
ShowFiles();
}

private void HideButton_Click(object sender, EventArgs e)
{
HideFiles();
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Hi,
Morten is right.

".Software\\Microsoft\\Windows\\CurrentVersion\\Ex plorer\\Advanced" should
be "Software\\Microsoft\\Windows\\CurrentVersion\\Exp lorer\\Advanced".

All the Best,
Phil.

"Morten Wennevik" wrote:
Hi Markali,

Don't worry, we all have experienced this error and do so still :)
The error means you have an reference that is null and you try to do
something with it.

I suspect it is the ...

RegistryKey MyKey

.... part. OpenSubKey returns null when it fails to aquire the subkey, so
when you try to do ...

MyKey.SetValue("Hidden",2);
MyKey.Close();

.... it will choke.

put something like this in the method

if(MyKey != null)
{
MyKey.SetValue("Hidden",2);
MyKey.Close();
}

On Sat, 05 Mar 2005 03:12:35 -0800, Markali A. <ya****@hotmail.com> wrote:


Hello,

I'm absolutely new to programming, so please don't be too hard on me...

I'm trying to make a simple form application to hide or show hidden
files. When I try to run the application I get a
"System.NullReferenceException: Object reference not set to an instance
of an object."
I found many topics related to this problem, but I still cannot figure
it out.

Here's the code:

partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void HideFiles()
{
RegistryKey MyKey = Registry.CurrentUser;
MyKey =
MyKey.OpenSubKey(".Software\\Microsoft\\Windows\\C urrentVersion\\Explore
r\\Advanced", true);
MyKey.SetValue("Hidden",2);
MyKey.Close();
}

private void ShowFiles()
{
RegistryKey MyKey;
MyKey = Registry.CurrentUser;
MyKey =
MyKey.OpenSubKey(".Software\\Microsoft\\Windows\\C urrentVersion\\Explore
r\\Advanced", true);
MyKey.SetValue("Hidden",1);
MyKey.Close();
}

private void ShowButton_Click(object sender, EventArgs e)
{
ShowFiles();
}

private void HideButton_Click(object sender, EventArgs e)
{
HideFiles();
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
1
by: Dean Bortell | last post by:
I have a program that encrypts a datetime objects "to long datetime string" string to and encrypted sequence. The problem is this: if I use the following source code it works (key is declared...
1
by: Dean Bortell | last post by:
I have a program that encrypts a datetime objects "to long datetime string" string to and encrypted sequence. The problem is this: if I use the following source code it works (key is declared...
0
by: Junkguy | last post by:
I need to be able to tell if the blank row of a DataGrid has been edited or not. In a DataGrid, you may notice that if you click on the asterisk row, the asterisk will change to a triangle, but...
0
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie = Request.Cookies("MyCookie") If Not cookie Is Nothing Then...
3
by: rashpal.sidhu | last post by:
Please help, this problem is driving me crazy !! I am using metaphone to create phonetic keys. When i run the module stand-a-lone it works fine. I'm trying to create a runner for informix...
3
by: Rafath | last post by:
Hello iam new to this forum but i hope this will help me in solving the case The error iam getting is "Registry editing has been disabled please contact your system administrator" mines is a...
2
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts"...
4
AnuSumesh
by: AnuSumesh | last post by:
Hi, One of our customer has following requirement: When user login to web site, some key should be added to client side registry and when he logs off, then that key should be deleted from...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.