Results 1 to 6 of 6

Thread: [RESOLVED] Object reference not set to an instance of an object

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Resolved [RESOLVED] Object reference not set to an instance of an object

    For some reason, on the line where I try to set the value of the key, I get the following error:

    Object reference not set to an instance of an object

    vb Code:
    1. Private Sub writeToKey(ByVal regValue As String)
    2.         Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
    3.         regkey.SetValue("Device", regValue, RegistryValueKind.String) 'error occurs here
    4.         regkey.Close()
    5.     End Sub

    What am I doing wrong?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Object reference not set to an instance of an object

    It means that Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows\Device") = Nothing

    Thus, regkey = Nothing, which means you can't set the value of a Nothing.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Object reference not set to an instance of an object

    In other words:
    the registrykey doesn't exist or you don't have the right permissions to open it.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  4. #4

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Object reference not set to an instance of an object

    Ok, I think what I need to do is change that line of code to:

    Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows\")

    But now it fails with the message that it cannot write to the key. Is there something wrong with the following line? I'm trying to change the value of the key HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device. This is the key for the default printer.

    regkey.SetValue("Device", regValue, RegistryValueKind.String) 'fails here

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Object reference not set to an instance of an object

    Are you sure you have permission to change that key? Is this being run on a Vista machine?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Object reference not set to an instance of an object

    Ooops! Forgot to use "True" at the end to make the key writable...

    Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Windows", True)

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