Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
 //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 25-Feb-13 8:25am
namerg389
Edited 25-Feb-13 8:30am
v2
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 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.
  Permalink  
Comments
namerg at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100