-2

I would like to read name and value pairs from Windows registry. I want to use a static class for it. The static class contains methods, what reads name/value pairs from Win Registry database and fill they into a Dictionary. I want to use this static method from my console application but I got a System.Null ReferenceExeption: 'Object reference not set to an instance of an object.' The codes:

   using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Win32;
namespace Winx64RegistryManipulator
{
    public enum RegistryAg
    {
        HKLM, //= HKEY_LOCAL_MACHINE
        HCU  //= HKEY_CURRENT_USER
    }
    public static class RegManipulator
    {
        #region Variables
        static RegistryAg regAg;
        static string path;
        static Dictionary<string, string> keyPairs; //this will contains the name/value pairs
        #endregion
        #region Constructors
        //Static class - no constuctor
        #endregion
        #region Public methods and functions
        public static Dictionary<string, string> ReadRegKeys(RegistryAg ag, string path)
        {
             RegistryKey key;

            if (ag == RegistryAg.HKLM) //HKEY_LOCAL_MACHINE
            {
                key = Registry.LocalMachine.OpenSubKey(path); //Open the Registry tree
                foreach (var item in key.GetValueNames())
                {
                    RegistryValueKind rkv = key.GetValueKind(item);
                    if (rkv == RegistryValueKind.Binary)
                    {
                        var value = (byte[])key.GetValue(item);
                        keyPairs.Add(item.ToString(), BitConverter.ToString(value));
                    }
                    else if (rkv == RegistryValueKind.DWord || rkv == RegistryValueKind.QWord)
                    {
                        var value = int.Parse(key.GetValue(item));
                        keyPairs.Add(item.ToString(), value.ToString());
                    }
                    else
                    {
                        keyPairs.Add(item.ToString(), key.GetValue(item).ToString());
                    }
                }
                return keyPairs;
            }
            else 
            {
                //more code
                return keyPairs;
            }
        }
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Winx64RegistryManipulator;
namespace Teszt
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs = RegManipulator.ReadRegKeys(RegistryAg.HKLM, "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Test");
            Console.ReadKey();
        }
    }
}
  • So you have a null, and are trying to do `null.WontWork()` or similar. Find out where (**this information is included in the stack trace**; read it). Then the problem area will be isolated and the issue can be explained / addressed: I'll wager two cents it's on a bad path/key being looked up (as the registry methods like to return `null` when things are not found). Using a *debugger* might be handy as well. – user2864740 Oct 29 '18 at 00:56
  • Also, `keyPairs` (the static variable; which is *different* from the `keyValuePairs` local variable that is useless created and discarded) is *never* assigned a value, so it might just be *that simple*.. **poof** when doing `keyPairs.Anything()`. Regardless- as a programmer, gotta learn to *follow evidence* to reduce/find problems so they can be fixed. – user2864740 Oct 29 '18 at 01:00

1 Answers1

0

You are missing Dictionary initialisation. Add the below code at the start of your method ReadRegKeys and you should be good to go.

keyPairs = new Dictionary<string, string>();

Hope this helps!

Responding to comment

You have initialized the dictionary at wrong location. Your code should look like this.

public static Dictionary<string, string> ReadRegKeys(RegistryAg ag, string path)
{
    RegistryKey key;
    keyPairs = new Dictionary<string, string>();
dj079
  • 1,389
  • 8
  • 14
  • Thanks! You give me a good idea! I investigate my static class again, I highlighted the variables, I was split the Binary and DWORD and QWORD types and I created a byte[] and int variables. – Zoltan Dobrogyinszki Oct 29 '18 at 08:29
  • Do you know, how do I add my modified code into question? I want to share my solution with you. – Zoltan Dobrogyinszki Oct 29 '18 at 08:38
  • You have initialized the keyPairs at wrong location. You need to initialize it inside the "ReadRegKeys" method, below "RegistryKey key;" – dj079 Oct 29 '18 at 09:15