使用Array并得到:“对象引用未设置为对象的实例."错误 [英] Using Array and get:"Object reference not set to an instance of an object." Error
问题描述
大家好,
我正在读取数据表中的行,并试图将信息存储在字符串数组中每一行的特定列下.但出现对象引用未设置为.."错误.
Hi folks,
I''m reading from rows of a datatable and I''m trying to store the info under a certain column for each row in an array of strings . But I get the"Object reference not set to.." error.
For index As Integer = 0 To SubTables.Count - 1
Dim RefDesgStr() As String = Nothing
Dim ii As Integer = 0
For Each row As DataRow In SubTables(index).Rows
RefDesgStr(ii) = row.Item("RefDesg") ' Got the error here
' some code checking RefDesgStr(ii) with all the prior strings in RefDesgStr()
ii +=1
Next row
index+=1
next
我很困惑,因为如果RefDesgStr是简单的字符串类型,我不会得到错误并覆盖每个循环,而不是字符串数组.
I''m confused cuz I didn''t get error if RefDesgStr was a simple string type and get overwritten each loop, instead of being an array of strings.
推荐答案
它不起作用,因为您声明了一个指向字符串数组的变量.您实际上并没有创建字符串数组.
您需要ReDim将数组保留为已知大小,以使其正常工作.
更好的是,删除数组并使用List(Of String)代替.这样一来,您根本无需费心调整数组的大小.
这个:
It doesn''t work because you declared a variable that will point to an array of strings. You didn''t actually CREATE an array of strings.
You need to ReDim Preserve the array to a known size in order for this to work.
Better yet, drop the array and use a List(Of String) instead. Then you don''t need to screw around with resizing an array at all.
This:
For index As Integer = 0 To SubTables.Count - 1
Dim RefDesgStr() As String = Nothing
Dim ii As Integer = 0
For Each row As DataRow In SubTables(index).Rows
ReDim Preserve RefDesgStr(ii)
RefDesgStr(ii) = row.Item("RefDesg") '' Got the error here
ii+=1
Next
Next
变成这个:
becomes this:
For index As Integer = 0 To SubTables.Count - 1
Dim RefDesgStr As New List(Of String)
Dim ii As Integer = 0
For Each row As DataRow In SubTables(index).Rows
RefDesgStr.Add(row.Item("RefDesg"))
Next row
Next
数组类 [ ^ ]
VB.NET中的数组 [
Array Class[^]
Arrays in VB.NET[^]
如果您明确将RefDesgStr
初始化为RefDesgStr >?这就是所有问题.相反,您可以执行以下操作:
How could you possibly de-referenceRefDesgStr
if you explicitly initialized it asNothing
? This is all the problem. Instead, you could do something like:
Dim RefDesgStr As String() = New String(SubTables.Count) {}
您认为在每个循环中都被覆盖"是完全错误的.您正在使用字符串类型的数组.当为它的元素分配一个字符串值时,该元素可以为null,但不能为整个对象.如果您不了解它,恐怕您宁愿几乎从一开始就学习语言和编程,以了解什么是类型,引用或值类型,引用,实例(对象),数组,数组元素及相关内容非常基本的概念.
下次,请始终在调试器下运行代码,并在引发异常之前检查变量.这种错误是最容易修复的错误之一.
You consideration of "overridden in each loop" is totally wrong. You are working with the array of string type. When you assign a string value it its element, the element can be null, but not the whole object. If you are not getting it, I''m afraid you would rather learn language and programming nearly from the very beginning, to understand what is the type, reference or value type, reference, instance (object), array, array element and related very basic concepts.
Next time, always run your code under debugger and examine variables before the exception is thrown. This type of bug is one of the easiest to fix.
这篇关于使用Array并得到:“对象引用未设置为对象的实例."错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!