Error Reading Values From The Registry System.nullreferenceexception
SQL Server 2014 Express resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Ask a question Quick access Forums home Browse forums users FAQ Search related threads Remove From My Forums Answered by: System.NullReferenceException when trying to read values from the registry. Visual Studio Languages , .NET Framework > Visual Basic Question 0 Sign in to vote Hey guys, Since I reinstalled Windows 7 yesterday, I've been getting a weird System.NullReferenceException exception when the program tries to read a string value from the registry. It was working fine on Saturday, and on the morning of Sunday (Before I reinstalled Windows) and I don't know why it's mucking around. Basically, what the code does, is it checks if a certain value is present in the registry and if that value exists, then the program will activate all the pro features. This occurres at each startup. I know it's not the most efficient way of doing things, but I guess it doesn't matter if this program takes about a minute to start properly (It also checks some other things at startup, so...) Here's the code: Luckily I'm using dimmed values, so it doesn't really matter if you see the code, I guess.. Public Sub CheckPro() ' First, checks if a specific file exists. If the file exists, then program will check if it should activate Pro features. If File.Exists("C:\Universal_Android_Kit\AppFiles\Pro\License\License.Key.BeatsleigherXDA") Then If My.Computer.Registry.GetValue(regVal, "Bin", Nothing).ToString = "01010100 01101000 01101001 01110011 0100000 01110000 01110010 01101111 01100111 01110010 01100001 01101101 0100000 01101001 01110011 0100000 01101110 01101111 01110111 0100000 01101111 01100110 01100110 01101001 01100011 01101001 01100001 01101100 01101100 01111001 0100000 01101000 01100101 0100000 01000100 01101111 01101110 01100001 01110100 01100101 0101111 01010000 01110010 01101111 0100000 01110110 01100101 01110010 01110011 01101001 01101111 01101110 0101110 0100000 01010100 01101000 01100001 01101110 01101011 01110011 0100000 01100110 01101111 01110010 0100000 01101000 01100101 01101100 01110000 01101001 01101110 01100111 0100000 01110101 01110011 0100000 01101111 01110101 01110100 0100000 01100001 01101110 01100100 0100000 01110000 01101100 01100001 01111001 01101001 01101110 01100111 0100000 01100110 01100001 01101001 01110010 010
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it https://social.msdn.microsoft.com/Forums/en-US/278b96dd-dbfd-4f79-98f7-2f31422ce47a/systemnullreferenceexception-when-trying-to-read-values-from-the-registry?forum=vbgeneral only takes a minute: Sign up C# Exception reading registry: Null reference exception up vote 0 down vote favorite I'm trying to develop a method to read the programs installed on the machine. public void refreshProgramsFromWindows () { string SoftwareKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products"; RegistryKey rk = default(RegistryKey); rk = Registry.LocalMachine.OpenSubKey(SoftwareKey); //string skname = null; string sname = string.Empty; // New list http://stackoverflow.com/questions/16138448/c-sharp-exception-reading-registry-null-reference-exception from scratch this.installedSoftwareList = new List
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business http://stackoverflow.com/questions/18560491/c-sharp-unhandled-exception-system-nullreferenceexception Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C# - unhandled exception “System.NullReferenceException” up vote -3 down vote favorite When trying to find a registry key that does not exist it throws error reading an unhandled exception. It looks like when checkKey is returned null and it's trying to continue to .GetValue it throws the exception. public static string getDirectory(string path, string subpath) { if (checkKey(path).GetValue(subpath) != null) { return checkKey(path).GetValue(subpath).ToString(); } else { return null; } } I tried if (checkKey(path) != null & checkKey(path).GetValue(subpath) != null) but that didn't resolve the issue. public static RegistryKey checkKey(string key) { if (getBaseCurrent64().OpenSubKey(key) != null) { return getBaseCurrent64().OpenSubKey(key); } else if error reading values (getBaseLocal64().OpenSubKey(key) != null) { return getBaseLocal64().OpenSubKey(key); } return null; } a try catch would solve this but i feel like I'm doing this wrong. kind regards, c# exception error-handling share|improve this question asked Sep 1 '13 at 16:33 Seritin 265 2 Gnerally speaking: do not call a method twice. Call it once, put the return value into a variable and then decide what to do with it. –nvoigt Sep 1 '13 at 16:36 1 if (checkKey(path) != null && checkKey(path).GetValue(subpath) != null), with just one & the second part will still be executed, which throws an error. && "short circuits", meaning if the first isn't true, the second won't even be checked. –Corak Sep 1 '13 at 16:36 add a comment| 2 Answers 2 active oldest votes up vote 0 down vote accepted You can do a GetValue() when you return null. Try to change your code to public static string getDirectory(string path, string subpath) { RegistryKey key = checkKey(path); if (key != null && key.GetValue(subpath) != null) { return key.GetValue(subpath).ToString(); } else { return null; } } share|improve this answer answered Sep 1 '13 at 16:42 Marco 23827 add a comment| up vote 0 down vote You need to use the logical AND operator (&&) instead of the bit-wise AND operator (&), change your code to this: if (checkKey(path) != null