473,506 Members | 17,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does this test for null fail?

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
Nov 6 '06 #1
6 2105
does DBMil have == overloaded?

"Jim H" <ji**@nospam.nospamwrote in message
news:e9**************@TK2MSFTNGP04.phx.gbl...
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

Nov 6 '06 #2
Jan
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
"Jim H" <ji**@nospam.nospamskrev i en meddelelse
news:e9**************@TK2MSFTNGP04.phx.gbl...
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

Nov 6 '06 #3
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

"Michael C" <no****@nospam.comwrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
does DBMil have == overloaded?

"Jim H" <ji**@nospam.nospamwrote in message
news:e9**************@TK2MSFTNGP04.phx.gbl...
>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


Nov 6 '06 #4
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

"Jan" <ja**********@hotmail.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
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
"Jim H" <ji**@nospam.nospamskrev i en meddelelse
news:e9**************@TK2MSFTNGP04.phx.gbl...
>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


Nov 6 '06 #5
"Jim H" <ji**@nospam.nospamwrote in message
news:ub****************@TK2MSFTNGP02.phx.gbl...
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
Nov 6 '06 #6
Michael C <no****@nospam.comwrote:
"Jim H" <ji**@nospam.nospamwrote in message
news:ub****************@TK2MSFTNGP02.phx.gbl...
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. :-)
The alternative is to use object.ReferenceEquals...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1709
by: Don | last post by:
Can't figure this one out. The "validate" function below checks to make sure the field "names" is not null. "validate" actually calls "isEmpty", which scans the field. Here's the situation. ...
24
3764
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
11
2454
by: Ayende Rahien | last post by:
I've a really strange problem, in some part of my code I compare two strings (through object), and while I *know* that they equal each other, and in the watch window they do equal each other, then...
13
4999
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
10
2343
by: Gunnar G | last post by:
I'm having problem reading from the beginning of a file. Here is the code (more or less) ifstream codefin; ofstream codefout; while (not_annoyed)
3
22894
by: Adam J Knight | last post by:
Hi all, I am trying to test to see if a Session variable exists. This was my initial attempt, and doesn't work int intInstructorID = (Session.ToString() == null ?...
28
5689
by: lovecreatesbeauty | last post by:
Besides printing out for example " a.out: p113.c:8: main: Assertion `0' failed. Aborted " and a switch option NDEBUG, what other benefits does assert() provide in any scope of designing,...
89
5948
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
4
3890
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.