What do you do when a box comes up that says object reference not set to an instance of object?

Answer

in vb.net dim xyz as myclass=new class or dim xyz as new myclass you might have missed the new key word

Answer

Think of all data as an "object". An object can be something simple, like an integer, or can be something cmoplex (what we call an "aggregate"... a mixture of things, like the way comcrete is an aggregegate mixture of stuff)... such as a "Customer", with a name, ID Number, and account balance. An object is stored in memory, and is referred to or "Referenced" by a variable. Dependign on the language you are writing in, this relationship between the variable and the object (what we call an "instance" of an object.) may be very clear, or it may be somewhat ambiguous. Think of a an object variable like an index card that holds an address. If I hand you a blank index card, you indeed have something on which you can record an address, but you have no address-- The index card is an object reference, but at this moment it doesn't refer to anything.

Some variables, like an integer for example (simple built-in types in most languages) are usually automatically initialized to point to an instance of an object of the appropriate type. Some languages will then even initialize that object with a default value (like 0 for an integer in Basic). However, complex object types (the classes you define) are not automatically initialized to instances of an object. In Visual Basic, you insantiate an objec tiwht the SET keyword and use NEW to create a new instance:

Dim x as MyClass set x = new MyClass

In C#, You simply assign the object variable an instance of the class:

MyClass x; x = new MyClass();

You are receiving the "Object Reference Not Set to an Instance of an Object" because at some point in your program, you are trying to use or manipulate the data stored in an object variable, but the object isn't pointing to anything. This would be like getting in your car with the blank index card I gave you and trying to drive to an address that doesn't exist. I hope this helps! :)

Jim

Improve Answer Discuss the question "What do you do when a box comes up that says object reference not set to an instance of object?" Watch Question

First answer by ID3705927476. Last edit by Feaz. Contributor trust: 45 [recommend contributor]. Question popularity: 22 [recommend question]

Research your answer:

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > What do you do when a box comes up that says object reference not set to an instance of object?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
What is object reference?  Pictures and Object of reference?  How do you set up and hold an object?  Instance of an object in java language?  What is the inertia of an object at rest?  Whether the object is call by value or reference?