views:

83

answers:

4

I am a beginner at C# and I'm getting a NullReferenceException error on some simple code for handling a button click event. I've still got just a bit of code to add at the very end to actually display the value from "TcpAddr" on the messagebox. This will allow you run the program but clicking the button causes it to throw the error.

Also: Is it better practice to move the actual query out of the click event and just make the click event handle MessageBox.Show()?

using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

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

        private void button2_Click(object sender, EventArgs e)
        {
            RegistryKey RegKey = Registry.LocalMachine;
            RegKey = RegKey.OpenSubKey("SOFTWARE\\Altiris\\Client Service");
            object CurrDS = RegKey.GetValue("TcpAddr"); //This line causes the NRE Error
            MessageBox.Show("Current DS:");
        }
    }
}
+4  A: 

My guess is that

RegKey = RegKey.OpenSubKey("SOFTWARE\\Altiris\\Client Service"); 

Is returning a null, probably because that key doesn't exist.

Verify the key exists and the provided reg path is correct.

asawyer
The path exists fully as HKLM\SOFTWARE\Altiris\Client Service and the string I want is TcpAddr. In a batch file this would be written as REG QUERY "HKLM\SOFTWARE\Altiris\Client Service" /v TcpAddr
Caley Woods
A: 

If it's throwing a NRE it's because it can't find the value, make sure it's spelled correctly or that the previous line isn't also returning null.

vash47
+3  A: 

According to the documentation for OpenSubKey(), "If the specified subkey cannot be found, then null is returned." If a variable is null, calling a method on it will throw that exception.


"Is it better practice to move the actual query out of the click event and just make the click event handle MessageBox.Show()?"

If you take it out, it won't necessarily reflect the current value of the key if, for example, another program modifies it while your program is running. Depending on your program, this may be okay.

Steve M
Does this path need to fully represent the path to the value? ie- add \TcpAddr to the end? TcpAddr is a string not a key
Caley Woods
+4  A: 

If you are sure that the registry key actually exists (use Regedit.exe) then you've got a problem if you are running on the 64-bit version of Windows. A VS2010 project is forced to run in 32-bit mode by default, it sees another set of registry keys.

Project + Properties, Build tab, Platform Target = Any CPU. Repeat for the Release configuration.

Hans Passant
+1 for to bring in the notice of 64 bit configuration.
saurabh
Thank you, I am running this on 64bit Windows 7. I'll check this and report back.
Caley Woods
I'm not seeing this as being an option, this is VS2010 Express, is that a missing option in the express version?
Caley Woods
Yes it is missing. Sigh. Open your .csproj with a text editor like Notepad and change the setting by hand. It is the `<PlatformTarget>` element, change it from "x86" to "AnyCPU".
Hans Passant
This has fixed the issue.
Caley Woods