Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Registry Problems NullException

Registry Problems NullException

Scheduled Pinned Locked Moved C#
databasesysadminwindows-adminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StevenWalsh
    wrote on last edited by
    #1

    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 :)

    C N 2 Replies Last reply
    0
    • S StevenWalsh

      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 :)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      1 Reply Last reply
      0
      • S StevenWalsh

        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 :)

        N Offline
        N Offline
        NassosReyzidis
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • N NassosReyzidis

          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

          S Offline
          S Offline
          StevenWalsh
          wrote on last edited by
          #4

          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);

          C 1 Reply Last reply
          0
          • S StevenWalsh

            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);

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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 )

            S 1 Reply Last reply
            0
            • C Christian Graus

              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 )

              S Offline
              S Offline
              StevenWalsh
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups