By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,451 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,451 IT Pros & Developers. It's quick & easy.

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

P: n/a


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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.