Object reference not set to an instance of an object
-
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..
-
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..
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
-
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
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.. :(
-
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.. :(
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
-
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
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
-
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
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
-
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'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..
-
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..
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
-
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
thanks a bunch for your help and detailed explanation.;)