对象引用未设置为对象的实例

时间:2021-07-05 16:55:59

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? thanks..

我在VB.net中遇到错误“对象引用没有设置为对象的实例”。你能告诉我这个错误的原因是什么吗?谢谢..

8 个解决方案

#1


1  

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

sef,如果问题出在数据库返回结果上,我认为是在这种情况下:

   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 ...

编辑:添加代码格式标签...

#2


9  

The object has not been initialized before use.

该对象在使用前尚未初始化。

At the top of your code file type:

在代码文件的顶部键入:

Option Strict On
Option Explicit On

#3


4  

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.

“对象的实例”表示对象为空(或在VB中说“无”)。处理对象变量时,必须先创建该对象的实例,然后再引用它。

"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.

你所做的只是告诉编译器aPerson代表一个人,而不是代表什么人。

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!

希望有所帮助!

#4


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.

通常,在.NET运行时下,只要取消分配或赋值为Nothing(在VB.Net中为null,在C#中为null)的变量,就会发生这样的事情。

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:

Option Strict On和Option Explicit On将帮助检测可能发生这种情况的实例,但是可以从另一个函数调用获得null / Nothing:

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".

并且NullReferenceException是“未设置为对象实例的对象引用”的源。

#5


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

#6


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.

您可以在应用程序中放置日志记录机制,以便隔离错误原因。 Exception对象具有StackTrace属性,该属性是一个描述调用堆栈内容的字符串,最新的方法调用首先出现。通过查看它,您将获得有关可能导致异常的更多详细信息。

#7


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.

如果数据集中不存在tablename,则会获得对象“引用未设置为对象的实例”。对于数据集中的行或字段也是如此。

#8


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.

好吧,错误正在解释自己。由于您没有提供任何代码示例,我们只能在代码中的某处说,您正在使用Null对象执行某些任务。下面的代码示例我得到了相同的错误。

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

在代码中检查代码中的代码。由于您还没有给出代码示例,我们无法突出显示代码:)

#1


1  

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

sef,如果问题出在数据库返回结果上,我认为是在这种情况下:

   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 ...

编辑:添加代码格式标签...

#2


9  

The object has not been initialized before use.

该对象在使用前尚未初始化。

At the top of your code file type:

在代码文件的顶部键入:

Option Strict On
Option Explicit On

#3


4  

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.

“对象的实例”表示对象为空(或在VB中说“无”)。处理对象变量时,必须先创建该对象的实例,然后再引用它。

"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.

你所做的只是告诉编译器aPerson代表一个人,而不是代表什么人。

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!

希望有所帮助!

#4


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.

通常,在.NET运行时下,只要取消分配或赋值为Nothing(在VB.Net中为null,在C#中为null)的变量,就会发生这样的事情。

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:

Option Strict On和Option Explicit On将帮助检测可能发生这种情况的实例,但是可以从另一个函数调用获得null / Nothing:

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".

并且NullReferenceException是“未设置为对象实例的对象引用”的源。

#5


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

#6


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.

您可以在应用程序中放置日志记录机制,以便隔离错误原因。 Exception对象具有StackTrace属性,该属性是一个描述调用堆栈内容的字符串,最新的方法调用首先出现。通过查看它,您将获得有关可能导致异常的更多详细信息。

#7


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.

如果数据集中不存在tablename,则会获得对象“引用未设置为对象的实例”。对于数据集中的行或字段也是如此。

#8


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.

好吧,错误正在解释自己。由于您没有提供任何代码示例,我们只能在代码中的某处说,您正在使用Null对象执行某些任务。下面的代码示例我得到了相同的错误。

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

在代码中检查代码中的代码。由于您还没有给出代码示例,我们无法突出显示代码:)