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. Object reference not set to an instance of an object

Object reference not set to an instance of an object

Scheduled Pinned Locked Moved C#
debugginghelp
9 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.
  • I Offline
    I Offline
    Ice_Freez05
    wrote on last edited by
    #1

    excuse me guys...need help here...i'm having a trouble when i deployed my web service.it's giving me an error of "Object Reference not set to an instance of an object". on debug mode,it doesn't give me that error.but when i deploy it to other work station, it gives me that error.i can't trace where the error is coming from.can you help me guys.thanks a lot..

    OriginalGriffO 1 Reply Last reply
    0
    • I Ice_Freez05

      excuse me guys...need help here...i'm having a trouble when i deployed my web service.it's giving me an error of "Object Reference not set to an instance of an object". on debug mode,it doesn't give me that error.but when i deploy it to other work station, it gives me that error.i can't trace where the error is coming from.can you help me guys.thanks a lot..

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      You need to put in a try-catch block and log the full exception. You will then at least have a chance of spotting where the problem is. Other than that, it's all guess work!

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      I 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        You need to put in a try-catch block and log the full exception. You will then at least have a chance of spotting where the problem is. Other than that, it's all guess work!

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        I Offline
        I Offline
        Ice_Freez05
        wrote on last edited by
        #3

        Thank you very much for the reply..but i've already done that but to no avail,i still can't seem to find where the error is coming from.. :(

        OriginalGriffO 1 Reply Last reply
        0
        • I Ice_Freez05

          Thank you very much for the reply..but i've already done that but to no avail,i still can't seem to find where the error is coming from.. :(

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Does the exception not give a class and line number? Normally the Exception.ToString() lists the Exception.StackTrace, which gives this.

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          I 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Does the exception not give a class and line number? Normally the Exception.ToString() lists the Exception.StackTrace, which gives this.

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            I Offline
            I Offline
            Ice_Freez05
            wrote on last edited by
            #5

            thanks a lot for your help.i've found the error... public int getRequestedFunctionID(string strFunctCode) { SQLiteCommand SQLComm; SQLiteDataReader sqlDR = null; int intFuncNameID = 0; string strSQL = "SELECT FuncNameID " + "FROM tblFuncName WHERE FunctCode = '" + strFunctCode + "'"; try { cnn = new SQLiteConnection(strConn); cnn.Open(); SQLComm = new SQLiteCommand(strSQL, cnn); sqlDR = SQLComm.ExecuteReader(); sqlDR.Read(); if (sqlDR.HasRows) { intFuncNameID = Convert.ToInt32(sqlDR["FuncNameID"]); } } catch { intFuncNameID = 100; } finally { sqlDR.Dispose(); cnn.Close(); } return intFuncNameID; } this is where the error is coming from...can you tell me what's the problem with this code?thanks again... by the way, i'm calling that function in the main module..like so.. int intFunctID = cProc.getRequestedFunctionID(strFunctCode);

            modified on Wednesday, April 21, 2010 4:58 AM

            OriginalGriffO 1 Reply Last reply
            0
            • I Ice_Freez05

              thanks a lot for your help.i've found the error... public int getRequestedFunctionID(string strFunctCode) { SQLiteCommand SQLComm; SQLiteDataReader sqlDR = null; int intFuncNameID = 0; string strSQL = "SELECT FuncNameID " + "FROM tblFuncName WHERE FunctCode = '" + strFunctCode + "'"; try { cnn = new SQLiteConnection(strConn); cnn.Open(); SQLComm = new SQLiteCommand(strSQL, cnn); sqlDR = SQLComm.ExecuteReader(); sqlDR.Read(); if (sqlDR.HasRows) { intFuncNameID = Convert.ToInt32(sqlDR["FuncNameID"]); } } catch { intFuncNameID = 100; } finally { sqlDR.Dispose(); cnn.Close(); } return intFuncNameID; } this is where the error is coming from...can you tell me what's the problem with this code?thanks again... by the way, i'm calling that function in the main module..like so.. int intFunctID = cProc.getRequestedFunctionID(strFunctCode);

              modified on Wednesday, April 21, 2010 4:58 AM

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Ice_Freez05 wrote:

              what can i do if the function returns a null or 0 value?

              Either change the method you are calling, or check for null returns and behave appropriately! :laugh: It is perfectly valid to return a null; many methods do when they have no data to return. It is always worth checking for - there is very little performance overhead, and it saves a lot of hassle for everybody. Unless the guy who wrote the class didn't mention it in the documentation, in which case: hit him! :laugh: (Then check for nulls anyway...)

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              I 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Ice_Freez05 wrote:

                what can i do if the function returns a null or 0 value?

                Either change the method you are calling, or check for null returns and behave appropriately! :laugh: It is perfectly valid to return a null; many methods do when they have no data to return. It is always worth checking for - there is very little performance overhead, and it saves a lot of hassle for everybody. Unless the guy who wrote the class didn't mention it in the documentation, in which case: hit him! :laugh: (Then check for nulls anyway...)

                You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                I Offline
                I Offline
                Ice_Freez05
                wrote on last edited by
                #7

                i'm sorry...my mistake...the problem is not the function returning a null or zero value. i've pasted the code above your reply.can you please help me figured out the problem...thanks again..

                OriginalGriffO 1 Reply Last reply
                0
                • I Ice_Freez05

                  i'm sorry...my mistake...the problem is not the function returning a null or zero value. i've pasted the code above your reply.can you please help me figured out the problem...thanks again..

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Before we get to the problem, a couple of things! Firstly, when you post a code fragment, use the "code block" wdiget - it preserves teh formatting and makes it easier to read, like this:

                      public int getRequestedFunctionID(string strFunctCode)
                          {
                          SQLiteCommand SQLComm;
                          SQLiteDataReader sqlDR = null;
                  
                          int intFuncNameID = 0;
                  
                          string strSQL = "SELECT FuncNameID " +
                          "FROM tblFuncName WHERE FunctCode = '" + strFunctCode + "'";
                  
                          try
                              {
                              cnn = new SQLiteConnection(strConn);
                              cnn.Open();
                  
                              SQLComm = new SQLiteCommand(strSQL, cnn);
                              sqlDR = SQLComm.ExecuteReader();
                  
                  
                              sqlDR.Read();
                              if (sqlDR.HasRows)
                                  {
                                  intFuncNameID = Convert.ToInt32(sqlDR\["FuncNameID"\]);
                                  }
                  
                              }
                          catch
                              {
                              intFuncNameID = 100;
                  
                              }
                          finally
                              {
                              sqlDR.Dispose();
                              cnn.Close();
                              }
                          return intFuncNameID;
                          }
                  

                  Secondly, don't do DB access like that - always use parameterised queries. They help to make the code more readable, and remove the chances for a SQL Injection Attack.

                              cnn = new SQLiteConnection(strConn);
                              cnn.Open();
                              string strSQL = "SELECT FuncNameID FROM tblFuncName WHERE FunctCode = @FC";
                              SQLComm = new SQLiteCommand(strSQL, cnn);
                              SQLComm.Parameters.AddWithValue("@FC", strFunctCode);
                              sqlDR = SQLComm.ExecuteReader();
                  

                  Thirdly, don't use anonymous exceptions unless you document well why it shoudl be anonymous! Why not? Because when an exception occurs, it is really handy to know why it happened, so that a problem can be averted... At the least, explain why you can ignore the error, if any. OK, your problem: There are three ways that the method can return zero: 1) If strFunctCode does not exist in the DB. The reader will then return with no rows, and the value given when intFuncNameID was declared will be returned. 2) If strFunctCode exists in the DB, but has an FuncNameID of zero. 3) If strFunctCode is not what you think it is... Add some logging: If necessary put

                          if (intFuncNameID == 0)
                              {
                              LogToSomewhereTheDBI
                  

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  I 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Before we get to the problem, a couple of things! Firstly, when you post a code fragment, use the "code block" wdiget - it preserves teh formatting and makes it easier to read, like this:

                        public int getRequestedFunctionID(string strFunctCode)
                            {
                            SQLiteCommand SQLComm;
                            SQLiteDataReader sqlDR = null;
                    
                            int intFuncNameID = 0;
                    
                            string strSQL = "SELECT FuncNameID " +
                            "FROM tblFuncName WHERE FunctCode = '" + strFunctCode + "'";
                    
                            try
                                {
                                cnn = new SQLiteConnection(strConn);
                                cnn.Open();
                    
                                SQLComm = new SQLiteCommand(strSQL, cnn);
                                sqlDR = SQLComm.ExecuteReader();
                    
                    
                                sqlDR.Read();
                                if (sqlDR.HasRows)
                                    {
                                    intFuncNameID = Convert.ToInt32(sqlDR\["FuncNameID"\]);
                                    }
                    
                                }
                            catch
                                {
                                intFuncNameID = 100;
                    
                                }
                            finally
                                {
                                sqlDR.Dispose();
                                cnn.Close();
                                }
                            return intFuncNameID;
                            }
                    

                    Secondly, don't do DB access like that - always use parameterised queries. They help to make the code more readable, and remove the chances for a SQL Injection Attack.

                                cnn = new SQLiteConnection(strConn);
                                cnn.Open();
                                string strSQL = "SELECT FuncNameID FROM tblFuncName WHERE FunctCode = @FC";
                                SQLComm = new SQLiteCommand(strSQL, cnn);
                                SQLComm.Parameters.AddWithValue("@FC", strFunctCode);
                                sqlDR = SQLComm.ExecuteReader();
                    

                    Thirdly, don't use anonymous exceptions unless you document well why it shoudl be anonymous! Why not? Because when an exception occurs, it is really handy to know why it happened, so that a problem can be averted... At the least, explain why you can ignore the error, if any. OK, your problem: There are three ways that the method can return zero: 1) If strFunctCode does not exist in the DB. The reader will then return with no rows, and the value given when intFuncNameID was declared will be returned. 2) If strFunctCode exists in the DB, but has an FuncNameID of zero. 3) If strFunctCode is not what you think it is... Add some logging: If necessary put

                            if (intFuncNameID == 0)
                                {
                                LogToSomewhereTheDBI
                    
                    I Offline
                    I Offline
                    Ice_Freez05
                    wrote on last edited by
                    #9

                    thanks a bunch for your help and detailed explanation.;)

                    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