Results 1 to 4 of 4

Thread: Object reference not set to an instance of an object.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Colorado
    Posts
    83

    Object reference not set to an instance of an object.

    I keep getting this Error:

    An unhandled exception of type 'System.NullReferenceException' occurred in AdvancedReg.exe

    Additional information: Object reference not set to an instance of an object.

    With this code: (First Line after comment)
    VB Code:
    1. ' Delete the three values just added.
    2.         regProduct.DeleteValue("Path")
    3.         regProduct.DeleteValue("MajorVersion")
    4.         regProduct.DeleteValue("MinorVersion")
    5.         ' Delete the Product key, after closing it.
    6.         regProduct.Close()
    7.         regCompany.DeleteSubKey("JesterDevReg")
    8.         ' Delete the Company key, after closing it.
    9.         regCompany.Close()
    10.         regSoftware.DeleteSubKey("JesterDev")



    Edit: Here is the whole thing. Well more or less.



    VB Code:
    1. ' Here we define the RegistryKey objects for the registry hives.
    2.     'These arent all needed but I have them here as a refference for
    3.     'you to use when needed in your own apps.
    4.     Dim regClasses As RegistryKey = Registry.ClassesRoot
    5.     Dim regCurrConfig As RegistryKey = Registry.CurrentConfig
    6.     Dim regCurrUser As RegistryKey = Registry.CurrentUser
    7.     Dim regDynData As RegistryKey = Registry.DynData
    8.     Dim regLocalMachine As RegistryKey = Registry.LocalMachine
    9.     Dim regPerfData As RegistryKey = Registry.PerformanceData
    10.     Dim regUsers As RegistryKey = Registry.Users
    11.  
    12.  
    13.  
    14.     'Define Variables for 'Write'
    15.     Dim regSoftware As RegistryKey
    16.     Dim regCompany As RegistryKey
    17.     Dim regProduct As RegistryKey
    18.  
    19. 'Here we will add company and product keys under HKEY_LOCALMACHINE\
    20.         'HKEY_SOFTWARE.
    21.  
    22.         'First thing is to open the key.
    23.         regSoftware = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
    24.  
    25.         'Add the key, or just open it if it alread exists.
    26.  
    27.         regCompany = regSoftware.CreateSubKey("JesterDev")
    28.  
    29.  
    30.         'Add another key for the product name.. Or again, open it of it exists.
    31.  
    32.         regProduct = regCompany.CreateSubKey("JesterDevReg")
    33.  
    34.         'Create the three values under the product key in the hives.
    35.  
    36.         regProduct.SetValue("Path", "C:\AppPath\Bin")   ' a string value
    37.         regProduct.SetValue("MajorVersion", 2)              ' a numeric value
    38.         regProduct.SetValue("MinorVersion", 1)              ' a numeric value
    39.  
    40.         lblInfo.Text = "Values have been stored in the registry. It is advised that you delete them."
    41.  
    42.     End Sub
    43.  
    44.  
    45.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    46.  
    47.         ' Delete the three values just added.
    48.         regProduct.DeleteValue("Path")
    49.         regProduct.DeleteValue("MajorVersion")
    50.         regProduct.DeleteValue("MinorVersion")
    51.         ' Delete the Product key, after closing it.
    52.         regProduct.Close()
    53.         regCompany.DeleteSubKey("JesterDevReg")
    54.         ' Delete the Company key, after closing it.
    55.         regCompany.Close()
    56.         regSoftware.DeleteSubKey("JesterDev")
    57.  
    58.         lblInfo.Text = "Reg Keys have been deleted. Wise move."
    59.  
    60.     End Sub
    Last edited by Jes|er; Jan 16th, 2003 at 10:14 PM.
    12/32/84 - I need some code to make me a sandwhich.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Is the regProduct variable dimmed at class level, as there is one function header missing from your code its impossible to tell?

    It feels like it is but Im asking just to be sure.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Colorado
    Posts
    83
    Sorry about that. Yes it is dimmed at class level. I think I found the the problem however. If the reg keys are not present then an exception gets thrown.

    From what I have read it shouldnt throw an exception if a reg value is null. Have some ideas on this?
    Last edited by Jes|er; Jan 17th, 2003 at 08:08 PM.
    12/32/84 - I need some code to make me a sandwhich.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Yes, I think I have read something similar. But If I remember correctly I think I had to check if the value where null or not before doing deletes. Can't get to my projects at work as Im home now, but I'll try to remeber to check this when I get back.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width