Vb.net 为什么在其他结构中的结构数组';你重新申报了吗?

Vb.net 为什么在其他结构中的结构数组';你重新申报了吗?,vb.net,structure,nullreferenceexception,Vb.net,Structure,Nullreferenceexception,所以我有一个数据结构,它是开放的,有大量的数据存储。数据通过一系列结构存储。以下是我使用的结构和变量: Dim CurrentRec As Year Structure Year Dim Months() As Month End Structure Structure Month Dim Dayz() As Days Dim MiscExpend(,) As String End Structure Structure Days Dim Incomes(,

所以我有一个数据结构,它是开放的,有大量的数据存储。数据通过一系列结构存储。以下是我使用的结构和变量:

Dim CurrentRec As Year

Structure Year
    Dim Months() As Month
End Structure

Structure Month
    Dim Dayz() As Days
    Dim MiscExpend(,) As String
End Structure

Structure Days
    Dim Incomes(,) As String
    Dim Expenditures(,) As String
End Structure
现在我有一个小的测试子程序来测试它是否工作

Sub Test()
    ReDim CurrentRec.Months(12).Dayz(31).Incomes(4, 4)
    ReDim CurrentRec.Months(12).Dayz(31).Expenditures(4, 4)
    CurrentRec.Months(5).Dayz(12).Incomes(1, 1) = 5
End Sub

问题是,这段代码抛出了一个System.NullReferenceException错误,对于我这样的人来说,我无法找出原因。有人能帮忙吗?

您需要分别对每个阵列进行重拨:

ReDim CurrentRec.Months(12)
ReDim CurrentRec.Months(12).Dayz(31)
ReDim CurrentRec.Months(12).Dayz(31).Incomes(4, 4)
ReDim CurrentRec.Months(12).Dayz(31).Expenditures(4, 4)
CurrentRec.Months(5).Dayz(12).Incomes(1, 1) = 5
目前:

ReDim CurrentRec.Months(12).Dayz(31).Incomes(4, 4)
试图重新分配最后一个成员的收入(,),但此时
月数和
日数都未确定,因此出现错误。

您没有实例化任何内容。调暗或重拨数组不会实例化其任何元素,它只是修改其大小。数组的所有元素仍然为空

编辑:啊,我上面说的完全错了,我刚刚意识到为什么

没有保留的初始化。如果未指定“保留”,则ReDim将使用新数组元素的默认数据类型值来初始化这些元素

从。结构的默认值是结构,而不是null。结构不能为空