2

I have been getting an error in VB .Net

object reference not set to an instance of object.

Can you tell me what are the causes of this error ?

Bruno
  • 3,872
  • 4
  • 20
  • 37
sef
  • 5,115
  • 6
  • 23
  • 23

9 Answers9

10

The object has not been initialized before use.

At the top of your code file type:

Option Strict On
Option Explicit On
Nescio
  • 27,645
  • 10
  • 53
  • 72
  • Option Explicit, for those who may not know, forces declaration of variables. This means that if you fat-finger a variable name, the compiler will not assume you intended to create another variable, and it will complain. Option Strict means that if you want the type of a variable translated, then you'll do it manually. – Andrew Neely Jul 19 '11 at 16:31
7

Let's deconstruct the error message.

"object reference" means a variable you used in your code which referenced an object. The object variable could have been declared by you the or it you might just be using a variable declared inside another object.

"instance of object" Means that the object is blank (or in VB speak, "Nothing"). When you are dealing with object variables, you have to create an instance of that object before referencing it.

"not set to an " means that you tried to access an object, but there was nothing inside of it for the computer to access.

If you create a variable like

Dim aPerson as PersonClass

All you have done was tell the compiler that aPerson will represent a person, but not what person.

You can create a blank copy of the object by using the "New" keyword. For example

Dim aPerson as New PersonClass

If you want to be able to test to see if the object is "nothing" by

If aPerson Is Nothing Then
    aPerson = New PersonClass
End If

Hope that helps!

Andrew Neely
  • 908
  • 11
  • 19
5

sef, If the problem is with Database return results, I presume it is in this scenario:

   dsData = getSQLData(conn,sql, blah,blah....)
   dt = dsData.Tables(0)  'Perhaps the obj ref not set is occurring here

To fix that:

  dsData = getSQLData(conn,sql, blah,blah....)
   If dsData.Tables.Count = 0 Then Exit Sub
   dt = dsData.Tables(0)  'Perhaps the obj ref not set is occurring here

edit: added code formatting tags ...

torial
  • 13,085
  • 9
  • 62
  • 89
2

In general, under the .NET runtime, such a thing happens whenever a variable that's unassigned or assigned the value Nothing (in VB.Net, null in C#) is dereferenced.

Option Strict On and Option Explicit On will help detect instances where this may occur, but it's possible to get a null/Nothing from another function call:

Dim someString As String = someFunctionReturningString();
If ( someString Is Nothing ) Then
   Sysm.Console.WriteLine(someString.Length); // will throw the NullReferenceException
End If

and the NullReferenceException is the source of the "object reference not set to an instance of an object".

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
2

And if you think it's occuring when no data is returned from a database query then maybe you should test the result before doing an operation on it?

Dim result As String = SqlCommand.ExecuteScalar()   'just for scope'
If result Is Nothing OrElse IsDBNull(result) Then
    'no result!'
End If
Mark Glorie
  • 3,733
  • 3
  • 28
  • 31
1

You can put a logging mechanism in your application so you can isolate the cause of the error. An Exception object has the StackTrace property which is a string that describes the contents of the call stack, with the most recent method call appearing first. By looking at it, you'll have more details on what might be causing the exception.

Leon Tayson
  • 4,741
  • 7
  • 37
  • 36
1

When working with databases, you can get this error when you try to get a value form a field or row which doesn't exist. i.e. if you're using datasets and you use:

Dim objDt as DataTable = objDs.Tables("tablename")

you get the object "reference not set to an instance of object" if tablename doesn't exists in the Dataset. The same for rows or fields in the datasets.

Eduardo Campañó
  • 6,778
  • 4
  • 27
  • 24
0

Well, Error is explaining itself. Since You haven't provided any code sample, we can only say somewhere in your code, you are using a Null object for some task. I got same Error for below code sample.

Dim cmd As IDbCommand
cmd.Parameters.Clear()

As You can see I am going to Clear a Null Object. For that, I'm getting Error

"object reference not set to an instance of an object"

Check your code for such code in your code. Since you haven't given code example we can't highlight the code :)

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
0

In case you have a class property , and multiple constructors, you must initialize the property in all constructors.

Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43