error message

Started by
4 comments, last by Zahlman 18 years, 9 months ago
I use Microsoft Visual Studio.NET to program and i keep on getting an error message. I just made some changes (which, conveneintly, i dont fully remember so i cant just undo them) and tried to compile. The window pops up, the program completely screws up and this error message pops up:
Quote: An unhadled exception of type 'System.NullReferenceException' occured in pong.exe Additional information: Object reference not set to an instance of an object.
Can i please have someone tell me how to fix this?
Advertisement
That message is given for C# programs.

You have some variable that is typed to hold a reference to some sort of object. For some reason, it doesn't yet point to an actual object: this is the starting state for such variables (unless you initialize them appropriately). Then you try to do something with the object, which doesn't actually exist. This "throws an exception" (a way of reporting errors at runtime), and since nothing "catches" it (handles the problem), it falls all the way through and crashes. (nice metaphors you get with this stuff eh?)

On the plus side, your program is running, which means it at least compiles properly. ;)
Lol i completely....don't understand. Well, i kind of understand. But oh well, I cant expect every1 2 always answer my problem 4 me. Ill just inspect it a little more myself and learn something new :).
example of nullreference example:(assuming c#(also is about the same for vb.net or cpp.net or whatever you use))
public class exeption{public static void Main(){//what causes nullreference exeption:int i;int k=i+3;//whoops! we didnt initialize i(when it isnt init'ed, it points to null(which you cant operate on))}}
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
I don't use C# so apologies if I'm talking nonsense, but in Java (which I believe C# is somewhat based on) a null reference exception occurs when you declare an object, but you never actually construct it so your trying to use a non-existant object like so:

//declare a reference to an object of type foo, by default the reference points to NULL, not an actual object.Class Foo;//to solve the problem you must actually construct a new object and initialise Foo to point to it:Foo = new Class();
Quote:Original post by mike003
Lol i completely....don't understand. Well, i kind of understand. But oh well, I cant expect every1 2 always answer my problem 4 me. Ill just inspect it a little more myself and learn something new :).


That's fine. Writing properly increases my (and others') desire to be helpful, though. ;)

@supercoder74: Right idea, but you need actual objects to produce the problem - in C# as in Java, ints are "primitives", and thus are actual values even under the hood (and not pointers or references). Thus either i is 0, or it will fail to compile (asking you to provide an initialization before use - I don't know how C# treats this, but in Java a class member would be initialized to 0 while a local variable would have to be inited); the NullReferenceException is a runtime error.

@Spudder: That's the most common cause of it for beginners, yes, but I didn't want to assume anything ;) There are plenty of other ways to end up with a null reference.

This topic is closed to new replies.

Advertisement