1

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:");
        }
    }
}
Rob
  • 26,989
  • 16
  • 82
  • 98
Caley Woods
  • 4,707
  • 4
  • 29
  • 38
  • 4
    I would strongly recommend that you make your variables start off with lowercase. (For example, `regKey` instead of `RegKey`). – Steven Sudit Oct 11 '10 at 14:28

4 Answers4

5

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
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you, I am running this on 64bit Windows 7. I'll check this and report back. – Caley Woods Oct 11 '10 at 14:47
  • I'm not seeing this as being an option, this is VS2010 Express, is that a missing option in the express version? – Caley Woods Oct 11 '10 at 14:52
  • Yes it is missing. Sigh. Open your .csproj with a text editor like Notepad and change the setting by hand. It is the `` element, change it from "x86" to "AnyCPU". – Hans Passant Oct 11 '10 at 14:57
4

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
  • 17,642
  • 8
  • 59
  • 87
  • 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 Oct 11 '10 at 14:44
3

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
  • 8,246
  • 2
  • 25
  • 26
  • 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 Oct 11 '10 at 14:45
0

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.

Gustavo Puma
  • 993
  • 12
  • 27