Jump to content
  • Advertisement
Sign in to follow this  
mike003

error message

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites
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 :).

Share this post


Link to post
Share on other sites
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))

}
}



Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!