473,659 Members | 2,671 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.Diagnost ics.Trace.Write Line("\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.GetDat a(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 2113
does DBMil have == overloaded?

"Jim H" <ji**@nospam.no spamwrote in message
news:e9******** ******@TK2MSFTN GP04.phx.gbl...
Pid.DBMil lMil = null;
if (null == lMil)
System.Diagnost ics.Trace.Write Line("\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.GetDat a(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.no spamskrev i en meddelelse
news:e9******** ******@TK2MSFTN GP04.phx.gbl...
Pid.DBMil lMil = null;
if (null == lMil)
System.Diagnost ics.Trace.Write Line("\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.GetDat a(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(pR ight);

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******** ******@TK2MSFTN GP03.phx.gbl...
does DBMil have == overloaded?

"Jim H" <ji**@nospam.no spamwrote in message
news:e9******** ******@TK2MSFTN GP04.phx.gbl...
>Pid.DBMil lMil = null;
if (null == lMil)
System.Diagnost ics.Trace.Write Line("\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.GetDat a(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**********@h otmail.comwrote in message
news:Oi******** ******@TK2MSFTN GP02.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.no spamskrev i en meddelelse
news:e9******** ******@TK2MSFTN GP04.phx.gbl...
>Pid.DBMil lMil = null;
if (null == lMil)
System.Diagnost ics.Trace.Write Line("\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.GetDat a(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.no spamwrote in message
news:ub******** ********@TK2MSF TNGP02.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(pR ight);
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.no spamwrote in message
news:ub******** ********@TK2MSF TNGP02.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(pR ight);

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.Referenc eEquals...

--
Jon Skeet - <sk***@pobox.co m>
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
1715
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. <input> for "names" first existed in an html file. The test worked just fine there. If the field was filled, the test would pass. If the field was cleared after having been filled, it would fail as it should. The field is then passed, via POST,...
24
3797
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
2468
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 the comparision return false. See here for details: http://www.ayende.com/Blog/PermaLink,guid,f5aa1a11-1710-434f-bfe0-39ac876a6acb.aspx
13
5035
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
2373
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
22906
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 ? Convert.ToInt32(Request.QueryString): Convert.ToInt32(Session));
28
5718
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, debugging/coding and/or testing? Do you prefer the if statement of the language to the assert MACRO of the precompiler?
89
6035
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
4
3899
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' is not an appropriate modifier for the test() function - test() searches the entire string by default. However, I would expect it to degrade gracefully. Instead, I seem to be getting something as follows - using W3Schools handy page at :
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
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 we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.