Why does this test for null fail?

  • Thread starter Thread starter Jim H
  • Start date Start date
J

Jim H

Pid.DBMil lMil = null;
if (null == lMil)
System.Diagnostics.Trace.WriteLine("\r\nIt was NULL\r\n"); //-- this
never prints

That diag line never prints.
later on in a loop I do this:


for (int i = 0; i < liLoopCount; i++)
{
if(lMil == null)
lMil = new Pid.DBMil();

lMilCode.GetData(i); //--Causes a null refernce exception

//more code here
}


How do you check to see if a variable is referencing null?

Thanks in advance,
jim
 
Hi Jim.

Your *first* Null check should work. I think the problem is somewhere else.

First, are you certain you have a trace window to output to ? It may be that
your check actually succeeds but you cant see the output. Try setting a
breakpoint in the ...Writeline statement to see if it gets hit.

Second, in your loop it is to different variables you are referencing.
Although "lMil" is not null, it is perfectly valid for "lMilCode" to be
null, or for "GetData(i)" to trigger a null reference exception within it's
function body.

BR Jan
 
THANK YOU!!!!!

That was it. My code looked like this:
public static bool operator ==(Mil pLeft, Mil pRight)
{
if (null != (object)pLeft && null != (object)pRight)
return pLeft.IsCode(pRight);

return false;
}


I added the following and it works as expected now:
if (null == (object)pLeft && null == (object)pRight)
return true;

I've been pulling my hair out for hours. Dumb mistake on my part. I
totally forgot I overloaded that in the base class. I know I was doing
something wrong somewhere. Good call Michael.

Thanks again,
Jim
 
My bad, lMilCode should say lMil. I shorten var names before posting and
missed that one.
BTW It was a bug in the overloaded == operator in a base class. See my
response to Michael.

Thanks,
jim
 
Jim H said:
THANK YOU!!!!!

Glag I could be of assistance. I've had many a hair pulling episode myself
:-)
That was it. My code looked like this:
public static bool operator ==(Mil pLeft, Mil pRight)
{
if (null != (object)pLeft && null != (object)pRight)
return pLeft.IsCode(pRight);

That answers a question I had, how do you compare a value to null in the ==
overload without getting a stack overflow. I encountered this recently and
ended up taking out the overload I think, I'll have to try to find what code
I was working on and fix it properly now. :-)

Michael
 
Michael C said:
Glag I could be of assistance. I've had many a hair pulling episode myself
:-)


That answers a question I had, how do you compare a value to null in the ==
overload without getting a stack overflow. I encountered this recently and
ended up taking out the overload I think, I'll have to try to find what code
I was working on and fix it properly now. :-)

The alternative is to use object.ReferenceEquals...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top