473,404 Members | 2,174 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,404 software developers and data experts.

Obj reference not set...

The "object reference not set to an instance of an object" exception does
not give any info about the actual object reference that was null. Aside
from using more try/catch, is there some way to figure out which object
reference is causing the problem? Something descriptive, like in Debug
mode, but that is available in non-Debug mode? Is the IL that just executed
available? --Marty
Jul 21 '05 #1
4 1132
No, you can't get anything more descriptive.

If you are in debug mode, you should have access to things like the line
number, etc, which narrows it down quite a bit. It's hard for the runtime
to give more information, as this is just related to the variable name,
which has no real meaning at runtime.

"Marty McDonald" <mc******@wsdot.wa.gov> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
The "object reference not set to an instance of an object" exception does
not give any info about the actual object reference that was null. Aside
from using more try/catch, is there some way to figure out which object
reference is causing the problem? Something descriptive, like in Debug
mode, but that is available in non-Debug mode? Is the IL that just executed available? --Marty

Jul 21 '05 #2
Hello Marty,

Thanks for your post. As I understand, the problem you are facing is how to
figure out the problem which causes "object reference not set to an
instance of an object". Please correct me if there is any misunderstanding.
I now share the following information with you:

Generally speaking, this error is caused when you declare a class variable
and use it without instantiating it. Such problem is easy to be identified
from the call stack when the exception throws. As you know, we are able to
dump the call stack in try/catch. Without try/catch code, you need to get
the symbole file (.pdb file) of the application, attach VS .NET debugger to
the process of it, and then we can view the call stack. Another method is
to use a tool say, ADPlus, to create a dump file when a process crashes,
open the dump file in a debugger say, windbg, to check the probolem.

In the meantime, I believe the following MSDN article is very helpful for
debugging .NET application:

Production Debugging for .NET Framework Applications
http://msdn.microsoft.com/library/de...us/dnbda/html/
DBGch01.asp

Please feel free to let me know if you have any problems or concerns.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #3
At non-debug mode, you should have worked these things out :).

Seriously, the way to do it is to add checks for objects that might be null.
If you have a method that takes an argument, and the arg should not be null,
check, and throw an ArgumentNullException. If nulls are appearing
"randomly" throughout your code, see where the object is coming from.
Perhaps you're calling a method that returns a null on failure.

Splitting up expressions can help too, (for instance, doing:
myOBject.Param.SomeList[index].Property =
anotherList[index].SomeObject.Property.ToString() has a lot of places that
could be null). Separating into multiple expressions can make it simpler to
find out where something's going wrong.

-mike
MVP

"Marty McDonald"
<mc******@wsdot.wa.gov> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
The "object reference not set to an instance of an object" exception does
not give any info about the actual object reference that was null. Aside
from using more try/catch, is there some way to figure out which object
reference is causing the problem? Something descriptive, like in Debug
mode, but that is available in non-Debug mode? Is the IL that just executed available? --Marty

Jul 21 '05 #4
OK - thanks everyone! I do know why these kinds of errors happen, and I
know how to try/catch them. My main point was that I was wondering if there
was any extra info available (such as via reflection) to help determine the
actual type of the null reference.

Your responses confirm that there really is no other way (besides good
coding practices, try/catch, checking args, etc) to avoid the error, and
there is no extra CLR information available about such exceptions.

Thank you for your information. --Marty
Jul 21 '05 #5

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
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.