Registry Problems NullException
-
I'm trying to query the registry on a remote server to verify a value. using the code
RegistryKey RemoteKey; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); string value = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree).GetValue("1.3", 0).ToString();
When i run it i see this: A first chance exception of type 'System.NullReferenceException' occurred in LabMonitor.exe A first chance exception of type 'System.NullReferenceException' occurred in LabMonitor.exe and the exception message is: "Object reference not set to an instance of an object" Please help :) -
I'm trying to query the registry on a remote server to verify a value. using the code
RegistryKey RemoteKey; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); string value = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree).GetValue("1.3", 0).ToString();
When i run it i see this: A first chance exception of type 'System.NullReferenceException' occurred in LabMonitor.exe A first chance exception of type 'System.NullReferenceException' occurred in LabMonitor.exe and the exception message is: "Object reference not set to an instance of an object" Please help :)StevenWalsh wrote:
and the exception message is: "Object reference not set to an instance of an object"
You're bound to get that when you chain together calls without first checking the return values. I'd assume that OpenSubKey is failing, but you should step through and check it yourself, and write more defensive code in future.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I'm trying to query the registry on a remote server to verify a value. using the code
RegistryKey RemoteKey; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); string value = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree).GetValue("1.3", 0).ToString();
When i run it i see this: A first chance exception of type 'System.NullReferenceException' occurred in LabMonitor.exe A first chance exception of type 'System.NullReferenceException' occurred in LabMonitor.exe and the exception message is: "Object reference not set to an instance of an object" Please help :)Hello Steven, either the RemoteKey is null or the "RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree)" is null change your code from:
RegistryKey RemoteKey; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); string value = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree).GetValue("1.3", 0).ToString();
to:RegistryKey RemoteKey; RegistryKey RemoteSubKey; string value; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); if (RemoteKey != null) { RemoteSubKey = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed",RegistryKeyPermissionCheck.ReadSubTree); if (RemoteSubKey != null) { value = RemoteSubKey.GetValue("1.3", 0).ToString(); } } if (value != null && value.Length>0) { //The SubKey exist do your staff with the value }
GanDad
-
Hello Steven, either the RemoteKey is null or the "RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree)" is null change your code from:
RegistryKey RemoteKey; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); string value = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed", RegistryKeyPermissionCheck.ReadSubTree).GetValue("1.3", 0).ToString();
to:RegistryKey RemoteKey; RegistryKey RemoteSubKey; string value; string RemoteComputer = LabName + "isp0001"; RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer); if (RemoteKey != null) { RemoteSubKey = RemoteKey.OpenSubKey("SOFTWARE\\Twrap\\Packages\\Completed",RegistryKeyPermissionCheck.ReadSubTree); if (RemoteSubKey != null) { value = RemoteSubKey.GetValue("1.3", 0).ToString(); } } if (value != null && value.Length>0) { //The SubKey exist do your staff with the value }
GanDad
Hi, i've tried your code, and i'm still getting the exception here is where it is stopping RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer);
-
Hi, i've tried your code, and i'm still getting the exception here is where it is stopping RemoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteComputer);
Breaking it down will just help you work out where the problem is. It won't change that you are making a call which is returning null, and not checking it.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Breaking it down will just help you work out where the problem is. It won't change that you are making a call which is returning null, and not checking it.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
Breaking it down will just help you work out where the problem is. It won't change that you are making a call which is returning null, and not checking it.
i broke it down some more and narrowed to problem down to the string "value" i made value an object datatype and it now works.