Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello,

I am trying to read and create a key, name and its value, but I am getting the following error.

The code verifies the existence of the key TabbedBrowsing and its name Enabled with the respective value 0.
If TabbedBrowsing does not exist create it.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at UPI_IE8_GE_Settings.Program.Main(String[] args) in C:\Users\roserog\documents\visual studio 2010\Projects\UPI_IE8_GE_Settings\UPI_IE8_GE_Settings\Program.cs:line 140

Thanks for your help.

This is the code:
C#
//TABS/DISABLE TABBED BROWSING
           Console.WriteLine("");
           RegistryKey subkey3 = key.OpenSubKey("Software\\Microsoft\\Internet Explorer", true);
           RegistryKey subkey3a = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TabbedBrowsing", true);
           string[] names3 = subkey3.GetSubKeyNames();
           string[] names3a = subkey3a.GetValueNames();
           string YesTabbedBrowsingKeyExists = "";
           string NoTabbedBrowsingKeyExists = "";
           string YesEnabledNameExists = "";
           string NoEnabledNameExists = "";

           if (subkey3a == null)
           {
               Console.WriteLine("WOW, YOU ARE EMPTY");
               System.Threading.Thread.Sleep(10000);
               return;
           }
           foreach (string name in names3)
           {
               if (name.Equals("TabbedBrowsing", StringComparison.CurrentCulture))
               {
                   // Found TabbedBrosing
                   YesTabbedBrowsingKeyExists = "1";
                   break;
               }
               else
               {
                   // No Found TabbedBrowsing
                   NoTabbedBrowsingKeyExists = "1";
               }
           }
           if (YesTabbedBrowsingKeyExists == "1")
           {
               Console.WriteLine("YES Found TabbedBrowsing");
               foreach (string name in names3a)
               {
                   if (name.Equals("Enabled", StringComparison.CurrentCulture))
                   {
                       // Found TabbedBrowsing Enabled
                       YesEnabledNameExists = "1";
                       break;
                   }
                   else
                   {
                       // No Found TabbedBrowsing Enabled
                       NoEnabledNameExists = "1";
                   }
               }
               if (YesEnabledNameExists == "0")
               {
                   Console.WriteLine("Found TabbedBrowsing and Enabled");
                   int myValue3a = (int)subkey3a.GetValue("Enabled");
                   if (myValue3a == 0)
                   {
                       Console.WriteLine("Recommended TABBED BROWSING value: {0} Unchecked Tabbed Browsing", myValue3a);
                   }
                   else
                   {
                       Console.WriteLine("Setting the right GE Recommendation");
                       subkey3a.SetValue("Enabled", "0", RegistryValueKind.DWord);
                   }
               }
               if (NoEnabledNameExists == "1")
               {
                   Console.WriteLine("NO Found Enabled, creating it ");
                   Console.WriteLine("Setting the right GE Recommendation");
                   subkey3a.SetValue("Enabled", "0", RegistryValueKind.DWord);
               }
           }
           if (NoTabbedBrowsingKeyExists == "1")
           {
               Console.WriteLine("NO Found TabbedBrowsing, creating it ");
               Console.WriteLine("Setting the right GE Recommendation");
               subkey3.CreateSubKey("TabbedBrowsing");
               subkey3a.SetValue("Enabled", "0", RegistryValueKind.DWord);
           }
Posted
Updated 25-Feb-13 7:30am
v2
Comments
Sergey Alexandrovich Kryukov 25-Feb-13 13:56pm    
In what line?
Anyway, did you try to execute it under the debugger? Such things are easy to detect and resolve...
—SA

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.


For example, a potential line to raise it could be:
C#
int myValue3a = (int)subkey3a.GetValue("Enabled");


So, please DEBUG and it will tell you exact line and the object causing the error. Handle the same.
 
Share this answer
 
Comments
namerg 25-Feb-13 14:22pm    
I see that I have an error that says
"The name "names3a" does not exist in the current context.

I think is because subkey3a reg path does not exist on
string[] names3a = subkey3a.GetValueNames()

Am i right?

Thanks for your help.
Sandeep Mewara 25-Feb-13 14:51pm    
Probably so. You have the code line and access to registry to find out the issue and handle it. Go ahead.
namerg 25-Feb-13 14:22pm    
And that is Line 140

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