none
System.NullReferenceException: 'Object reference not set to an instance of an object.' RRS feed

  • Question

  • Heres the code halp plz 

            public static void checkssettings()
            {
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey key = rk.OpenSubKey(@"SOFTWARE\Kosmik\Settings\");
                if (key != null)
                {

                    key.SetValue("Theme", "Dark");
                    theme = (key.GetValue("Theme")).ToString();


                }
                else
                {

                    //key.SetValue("Theme", null);
                    key.SetValue("Theme", "Dark");

                }
            }

    Name Value Type
    $exception {"Object reference not set to an instance of an object."} System.NullReferenceException


    Thursday, July 25, 2019 3:36 AM

Answers

  • Try this.

                RegistryKey rk = Registry.LocalMachine;
                RegistryKey key = rk.OpenSubKey(@"SOFTWARE\Kosmik\Settings\");
                if (key != null)
                {
                    theme = (key.GetValue("Theme")).ToString();
                }
                else
                {
                    key = rk.CreateSubKey(@"SOFTWARE\Kosmik\Settings\");
                    if(key != null
                    {
                       key.SetValue("Theme", "Dark");
                    }
    
                }

    Thursday, July 25, 2019 4:07 AM

All replies