Click here to Skip to main content
15,887,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have c# application which include code to retrieve registry values and check it's values.registry values stored in following manner:

MainKey:
Name:user123
Isregistered:no

however if Isregistered returns no,it will display appropriate message.
i am getting error like this:---

Object reference not set to an instance of an object.

C# Code:

C#
RegistryKey se =  Registry.CurrentUser.OpenSubKey(@"MainKey", true);
           string currentKey;
            currentKey = reg.GetValue("Isregistered", true).ToString();
           if (currentKey == "Yes")
           {
               Console.WriteLine(currentKey.ToString());

           }
           else
           {
               Console.WriteLine(currentKey.ToString());
    }


i am getting error on currentKey =reg.GetValue("Isregistered", true).ToString();
Posted
Updated 3-Jan-12 0:47am
v3

The problem is that it could be either part of that:
C#
reg.GetValue("Isregistered", true)
reg could be null - you don't show it being defined or initialised
reg.GetValue("Isregistered", true).ToString()
If reg is ok, then GetValue could be returning null.
Or, it could be in your GetValue method itself.

Break it up, and single step throung in the debugger:
C#
if (reg != null)
    {
    object o = reg.GetValue("Isregistered", true);
    currentKey = o.ToString();
    }
Then you stand a chance of working out which is causing the problem.
 
Share this answer
 
you can pass Isregistered value in Read method for access Registry value as
Read("Isregistered ");

have a look at method declaration
public string Read(string KeyName)
{
    // Opening the registry key
      RegistryKey rk = baseRegistryKey ;
    // Open a subKey as read-only

    RegistryKey sk1 = rk.OpenSubKey(subKey);
    // If the RegistrySubKey doesn't exist -> (null)

    if ( sk1 == null )
    {
        return null;
    }
    else
    {
        try 
        {
       // If the RegistryKey exists I get its value or null is returned.        
         return (string)sk1.GetValue(KeyName.ToUpper());
        }
        catch (Exception e)
        {
            ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
            return null;
        }
    }
}



Reference Link :- Read Registry using C#[^]
 
Share this answer
 
i got solution.Thanks for help.


RegistryKey reg =  Registry.CurrentUser.OpenSubKey(@"MainKey", true);
            string currentKey;
             
             if (reg != null)
             {
                 RegistryKey rKey = reg.OpenSubKey("Isregistered", true);
                 object o = rKey.GetValue("Isregistered");
                 currentKey = o.ToString();
                 
           
                Console.WriteLine(currentKey.ToString());
              }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900