vb.net 未将对象引用设置为对象的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/131053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
object reference not set to an instance of object
提问by sef
I have been getting an error in VB .Net
我在VB .Net 中遇到错误
object reference not set to an instance of object.
未将对象引用设置为对象的实例。
Can you tell me what are the causes of this error ?
你能告诉我这个错误的原因是什么吗?
回答by Nescio
The object has not been initialized before use.
该对象在使用前尚未初始化。
At the top of your code file type:
在代码文件类型的顶部:
Option Strict On
Option Explicit On
回答by torial
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 ...
编辑:添加了代码格式标签...
回答by Andrew Neely
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 instanceof that object before referencing it.
“instance of object”表示对象是空白的(或者用VB来说,“ Nothing”)。当您处理对象变量时,您必须在引用它之前创建该对象的实例。
"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 whatperson.
您所做的只是告诉编译器 aPerson 将代表一个人,而不是代表什么人。
You can create a blank copy of the object by using the "New" keyword. For example
您可以使用“New”关键字创建对象的空白副本。例如
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!
希望有帮助!
回答by Mark Glorie
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
回答by Blair Conrad
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# 中)被取消引用时,就会发生这样的事情。
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 NullReferenceExceptionis the source of the "object reference not set to an instance of an object".
并且NullReferenceException是“对象引用未设置为对象的实例”的来源。
回答by Leon Tayson
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 属性,它是一个描述调用堆栈内容的字符串,最近的方法调用最先出现。通过查看它,您将获得有关可能导致异常的原因的更多详细信息。
回答by Eduardo Campa?ó
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.
如果数据集中不存在表名,则会得到对象“未将引用设置为对象的实例”。数据集中的行或字段也是如此。
回答by Menuka Ishan
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 :)
检查您的代码中是否有此类代码。由于您没有给出代码示例,我们无法突出显示代码:)
回答by Mohammed Yassine CHABLI
In case you have a class property , and multiple constructors, you must initialize the property in all constructors.
如果您有一个类属性和多个构造函数,则必须在所有构造函数中初始化该属性。