473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Null reference makes no sense

My stack trace contains:
System.NullReferenceException: Object reference not set to an instance
of an object.

but the line of code it refers to looks like this:
if (null == unit || ! unit.Enabled)
return;
Am I just missing something? The short-circuit evaluation of this
line should never allow the "! unit.Enabled" to be tested if "null ==
unit".

I've seen this fairly randomly. I can't reproduce it every time. I'm
willing to bet that it's a memory corruption somewhere.

Any tips on debugging this weirdness?

Marshall Belew
Nov 16 '05 #1
2 1587
Marshall Belew <mb****@koiosworks.com> wrote:
My stack trace contains:
System.NullReferenceException: Object reference not set to an instance
of an object.

but the line of code it refers to looks like this:

if (null == unit || ! unit.Enabled)
return;

Am I just missing something? The short-circuit evaluation of this
line should never allow the "! unit.Enabled" to be tested if "null ==
unit".

I've seen this fairly randomly. I can't reproduce it every time. I'm
willing to bet that it's a memory corruption somewhere.
Hmm... I wouldn't make any bets just yet.
Any tips on debugging this weirdness?


Well, what is "unit" in this case? If it overloads the == operator,
that could be the problem. Are you *absolutely* sure it's actually
occurring on this line, rather than near it? Could you post some more
code?
By the way, I'd suggest writing tests in the more natural way of

if (unit==null || !unit.Enabled)

by the way - I suspect I'm not alone in finding this considerably
easier to use. The problem of accidental assignment is gone in C#
(unless you compare booleans with true and false directly) as it's
stricter about what the type of expression of the "if" statement can
be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Are you using multiple threads? If some other thread would set unit to null
right after the "null == unit" test but before the "! unit.Enabled" request,
that line would throw an exception (but not every time).
Is it possible the "Enabled" is actually a property, and that the exception
is thrown inside the "get" function?
What is "unit" anyway? Is it a local/member/instance variable? A property?

Debugging tips:
1. Don't panic
2. Let the debugger break on exceptions, and see if "unit" is null when that
exception is fired. Maybe make a local copy of the variable before the
assignment (or somewhere else) to see if multithread- or memory corruption
issues could explain the behaviour.
3. Split the if in two lines (one if for the null-check, one for the
'enabled' check), to see for sure which one crashes.
4. Finally (if everything else fails) remove every piece of code from your
app until the error disappears...

Niki

"Marshall Belew" <mb****@koiosworks.com> wrote in
news:c8**************************@posting.google.c om...
My stack trace contains:
System.NullReferenceException: Object reference not set to an instance
of an object.

but the line of code it refers to looks like this:
if (null == unit || ! unit.Enabled)
return;
Am I just missing something? The short-circuit evaluation of this
line should never allow the "! unit.Enabled" to be tested if "null ==
unit".

I've seen this fairly randomly. I can't reproduce it every time. I'm
willing to bet that it's a memory corruption somewhere.

Any tips on debugging this weirdness?

Marshall Belew

Nov 16 '05 #3

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

Similar topics

3
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
4
by: Ken Durden | last post by:
I'm looking for a way to simulate the following kind of C code: --- C++ --- void F( double * array, double * fMean, double * fMedian, double * fMax, double * fMin ); --- C# --- void F(...
7
by: Kevin Cline | last post by:
Why, oh why is it necessary to test an event for null before raising it? Why isn't that case handled automatically, instead of forcing developers to write three lines of wasted boilerplate code...
5
by: Boniek | last post by:
Hi I define a public property in a new form and I can see this property in table of Properties in Visual. How I can hide this property to see only in code ? Thank's Boniek
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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,...
0
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...

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.