Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
Hi there, I've got a problem. I'm new to this site and a beginner so simple help would be great thanks!

Object reference not set to an instance of an object and NullReferenceException was unhandled. that what comes up when my spaceship collides with a randomly generated asteroid, anyone? Ideas why this is not working?

If you require the code, just say!
Posted
Comments
Toli Cuturicu 9-Jan-11 15:03pm    
Yes of course.
OriginalGriff 9-Jan-11 15:33pm    
I moved your answer to a comment on both answers. Please try to use comments rather than answers, or it mucks the system up a bit!

Yes we will probably need some code, but don't paste it all! Just the relevant bits - the method the error occurs in, say.

Having said that, most of the time it is pretty simple to work out where the problem:

The error says "Object reference not set to an instance of an object"
That means that somewhere you are using an object and trying to work with a feild, method, or property, but the object itself is not set.

The problem will be something along the lines of myVariable.myMethod or myArray[index].myMethod

The error message (and the exception) will tell you which line it is occurring on. Look at it in the debugger and follow the variables until you find the problem.

You may find it helps to break everything out into separate variables on separate lines until it is just one tiny bit of code per line.

MyArray[myIndex].myMethod().myProperty = 0;

becomes:
MyObject my1 = MyArray[myIndex];
MyOtherObject my2 = my1.myMethod();
MyThirdObject my3 = my2.myProperty;
my3 = 0;


Try it - it is a skill well worth developing yourself!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jan-11 15:19pm    
Agree, my 5. This is a key: "it is a skill well worth developing yourself".
See my advice.
OriginalGriff 9-Jan-11 15:32pm    
The OP posted "thanks for your help, but I just discovered the problem, I had my asteroid set to "null", i got rid of that and its all working fine!

Thank you again"
If you see the exception like this, the problem is already 90% solved.
I used to see good amount of code where many exceptions were suppressed by writing exception handlers not re-throwing any exception up to the stack. (This can be done in some quite rare cases; in almost all cases an exception can be only suppressed on the very top of each thread or in the UI cycle when and only when it is reported by the UI.)

You have not done bad thing; great, keep this way!

With Visual Studio you simply need to run under debugger. In 99.99% (?) cases the Studio will simply slap the offending line of the code right into your face.

There is one delicate thing to know: Debugger exception handling should be set up properly: main menu -> Debug -> Exceptions (Ctrl+D, E). Usually, the default is optimal.

No Studio, or, worse, the problem is not reproduces under Studio?
Not to worry: write exception handler to write a full dump of exception somewhere (text file, system event log). It's important to write full exception stack and all inner exceptions, recursively.
 
Share this answer
 
v5
Comments
OriginalGriff 9-Jan-11 15:32pm    
The OP Posted: "thanks for your help, but I just discovered the problem, I had my asteroid set to "null", i got rid of that and its all working fine!

Thank you again"
Sergey Alexandrovich Kryukov 9-Jan-11 15:38pm    
I noticed that and put a comment to this OP reply, thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900