0

I am banging my head around a constant error "Object reference not set to an instance of an object" when reading the value of a registry key. please give me your suggestions of how to resolve.

string regkey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{A8EC0CC0-AD8D-4244-B080-424EDF7A7634}";
object thekey = Registry.LocalMachine.OpenSubKey(regkey).GetValue("DisplayName");
TraktorVersion = thekey.ToString();

where TraktorVersion is a string.

Blagovest
  • 21
  • 1

1 Answers1

0

Are you sure that key exist in the registry? Check that thekey is not being set to null.

string regkey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{A8EC0CC0-AD8D-4244-B080-424EDF7A7634}";
object thekey = Registry.LocalMachine.OpenSubKey(regkey).GetValue("DisplayName");    
if(thekey!=null)    
   TraktorVersion = thekey.ToString();
jbriggs
  • 353
  • 2
  • 10
  • The key is copied and pasted from the registry and DisplayName value has data inside. I made a test with GetValueNames and only 3 values were returned. In the key there are around 10 values which confuses me even more. – Blagovest Jan 14 '17 at 22:32
  • You may need elevated rights to see the other values. Try right clicking on your app and run as administrator – jbriggs Jan 14 '17 at 22:42
  • 2
    Is your application 32-bit running on 64-bit workstation? Then some keys may not be available and you should look in Software\Wow6432Node regkey. – VitaliyK Jan 14 '17 at 22:42
  • Hi jbriggs, the put if statement if (thekey != null) { TraktorVersion = thekey.ToString(); System.Windows.MessageBox.Show(TraktorVersion); } , compiled and ran with admin rights. the message box didn't appear – Blagovest Jan 14 '17 at 22:50
  • That's because the key didn't exist or is isn't accessible. @VitaliyK could be right also. What are you using to view the registry? – jbriggs Jan 14 '17 at 23:20
  • on the command prompt you can run reg query "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /reg:32 If that shows the same 3 values then you know it's a 32bit vs 64bit thing – jbriggs Jan 14 '17 at 23:28