How to solve "Object reference not set to an instance of an object?

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 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. :) Jim

wiki.answers.com Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.