System.NullException Error
-
Hey guys, I have this really weird issues and I can't seem to figure it out, the code list below run fine with no errors what so ever when I run it from a GUI application, but when i put it into a service and use it, it gives me a System.NullException error, but what is weird is that it gives me that error in the Finally Statement, but for some reason it doesn't even bother writing to the registry like it is suppose to, but it keep in mind the exact same code works if it is in a GUI application. I have labeled the two lines that the error is given at. Thanks for those who help. Travis
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using Microsoft.Win32; class VCS { private RegistryKey regKey; private SqlDataReader rdr = null; private SqlCommand cmd = null; private string procValue; private string connString private SqlConnection virasecConn = new SqlConnection(); public void executeStorProc(string procedureName) { try { openConn(); cmd = new SqlCommand(procedureName, virasecConn); cmd.CommandType = CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); while (rdr.Read()) { regKey = Registry.LocalMachine.OpenSubKey ("SOFTWARE\\VIRASEC, LLC\\VCS", true); procValue = rdr[0].ToString(); regKey.SetValue("Test", procValue); } } catch (Exception ex) { Console.WriteLine(ex.Source + "\n" + ex.StackTrace); } finally { **regKey.Close(); - Error occurs here rdr.Close(); - Error occurs here** closeConn(); } } public void openConn() { virasecConn.ConnectionString = connString; try { virasecConn.Open(); } catch (Exception ex) { Console.WriteLine(ex.Source + "\n" + ex.StackTrace); } } public void closeConn() { try { virasecConn.Close(); } catch (Exception ex) {
-
Hey guys, I have this really weird issues and I can't seem to figure it out, the code list below run fine with no errors what so ever when I run it from a GUI application, but when i put it into a service and use it, it gives me a System.NullException error, but what is weird is that it gives me that error in the Finally Statement, but for some reason it doesn't even bother writing to the registry like it is suppose to, but it keep in mind the exact same code works if it is in a GUI application. I have labeled the two lines that the error is given at. Thanks for those who help. Travis
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using Microsoft.Win32; class VCS { private RegistryKey regKey; private SqlDataReader rdr = null; private SqlCommand cmd = null; private string procValue; private string connString private SqlConnection virasecConn = new SqlConnection(); public void executeStorProc(string procedureName) { try { openConn(); cmd = new SqlCommand(procedureName, virasecConn); cmd.CommandType = CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); while (rdr.Read()) { regKey = Registry.LocalMachine.OpenSubKey ("SOFTWARE\\VIRASEC, LLC\\VCS", true); procValue = rdr[0].ToString(); regKey.SetValue("Test", procValue); } } catch (Exception ex) { Console.WriteLine(ex.Source + "\n" + ex.StackTrace); } finally { **regKey.Close(); - Error occurs here rdr.Close(); - Error occurs here** closeConn(); } } public void openConn() { virasecConn.ConnectionString = connString; try { virasecConn.Open(); } catch (Exception ex) { Console.WriteLine(ex.Source + "\n" + ex.StackTrace); } } public void closeConn() { try { virasecConn.Close(); } catch (Exception ex) {
Hi, your code is not safe. regKey initially is null, it is only some way down the try block it may get a non-null value, yet you use it without any checking in the finally block. Imagine OpenCon() fails, you'll drop into the finally without a valid regKey. remedy: perform a null-test in the finally. in future: add debug/log statements, or single-step through your code. You can easily solve this kind of problems yourself. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, your code is not safe. regKey initially is null, it is only some way down the try block it may get a non-null value, yet you use it without any checking in the finally block. Imagine OpenCon() fails, you'll drop into the finally without a valid regKey. remedy: perform a null-test in the finally. in future: add debug/log statements, or single-step through your code. You can easily solve this kind of problems yourself. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
I appreciate your help, I added the code to check for null values. Another question I am hoping maybe you can help with is, I added debug code to help me sort out any further issues and I noticed that the line that says: rdr = cmd.ExecuteReader(); fails to execute, it drops immediatley into the catch block, do you have any ideas why that may be happening. Thanks, Travis
-
I appreciate your help, I added the code to check for null values. Another question I am hoping maybe you can help with is, I added debug code to help me sort out any further issues and I noticed that the line that says: rdr = cmd.ExecuteReader(); fails to execute, it drops immediatley into the catch block, do you have any ideas why that may be happening. Thanks, Travis
Hi, when you catch an exception you should display and log the entire exception.ToString(), not just parts of it. That probably will tell you some. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.