Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.NullException Error

System.NullException Error

Scheduled Pinned Locked Moved C#
helpwindows-adminquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tcombs07
    wrote on last edited by
    #1

    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) {

    L 1 Reply Last reply
    0
    • T tcombs07

      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) {

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        T Offline
        T Offline
        tcombs07
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • T tcombs07

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups