Monday, March 26, 2012

Object reference not set to an instance of an object

Hi i get the following error when i try to execute a function, what could be causing this, and it is coming from the following line of code;

Line 588: // Ensure is not emptyLine 589: if (strWhere[lngIter, 0].Length > 1)
This is my code that im trying to execute;
staticpublicstring buildQuery(string strAction,string strTableName,string[,] strData,string[,] strWhere,string strWhereOverride,string strLimit,string strOffset,string strOrderBy)

{

strAction = (strAction);

// Dimension

string strSQL;

long lngIter;

string strPart1 =null;

string strPart2 =null;

// Build first part

strSQL = strAction +" ";

// Build Insert Query

if (strAction =="INSERT")

{

// Next part of query

strSQL = strSQL +"INTO " + strTableName +" ";

// Build data part of query

for (lngIter = 0; lngIter < strData.Length; ++lngIter)

{

// Ensure is not empty

if (strWhere[lngIter, 0].Length > 1)

{

this is what happening,

in the above code your are accessing the Length property of the Multi-dimensional array like this,

Pmillio:

// Ensure is not empty

if (strWhere[lngIter, 0].Length > 1)

{

if strWhere not set you'll get an error. your strWhere should always create using new keyword in the calling code. Check that out first.


Thanks for responding, is there any code i can change it too?


in your calling code, check that strWhere is set to null like this

buildQuery("strAction","strTableName", strData,null,...);

when this run error occurs in line

Pmillio:

if (strWhere[lngIter, 0].Length > 1)

so here check for null as,

if(strWhere !=null){if (strWhere[lngIter, 0].Length > 1)

Hi that seems to work, however i am now getting that error in this line;

 strSQL = strSQL + "(" + strPart1.Substring(1, Strings.Len((strPart1)) - 1) + ") values (" + strPart2.Substring(1, Strings.Len((strPart2)) - 1) + ")";


In your code, you set

strPart1 =null;

strPart2 =null;

so again, you without setting values for those variables, other than null, you calling Substring function. You cant do this because of the nulls you referring to.
And also, keep in mind, if you going to build a string like this way, by putting + operand, don't do this for long string concatenations.  Use string.concat() or System.Text.StringBuilder.Append() instead.

The latter one is more advisable for very long strings.


Thanks for your help so far, i hope you dont mind helping me. The latest error im now getting is;

" Exception Details: System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string."

And its on this same line;

 // ConcatenateLine 574: strSQL = strSQL + "(" + strPart1.Substring(1, Strings.Len((strPart1)) - 1) + ") values (" + strPart2.Substring(1, Strings.Len((strPart2)) - 1) + ")";Line 575: }

this is wat hapen there,

if either one strPart1 or strPart2 has "Hello" as value. Now u can notice there is 5 characters. in your Substring method if you set startIndex as 5 you'l get error(always starts with 0 not 1).

in the above code you use 1 for startIndex. if strPart1 has 0 length or empty value u'l get error.


So are you suggesting i change the 1 to a zero in the code;

strSQL = strSQL +"(" + strPart1.Substring(1,Strings.Len((strPart1)) - 1) +") values (" + strPart2.Substring(1,Strings.Len((strPart2)) - 1) +")";

0 comments:

Post a Comment