对象引用未设置为实例错误类

时间:2014-09-03 18:00:07

标签: vb.net class new-operator

我有这堂课:

Imports System.Data
Public Class SqlToDataset
#Region "Partial Class DataFields"
#End Region
    Public Tables As List(Of DataTable)
    Public Structure SqlTable
        Dim DataFields As List(Of DataFields)
        Dim Rows As List(Of DataRow)
    End Structure
    Public SqlTables As List(Of SqlTable)
    Public Sub New()
        Tables = New List(Of DataTable)
        SqlTables = New List(Of SqlTable)
    End Sub
    Public Sub LoadData()
        Dim OneSqlTable As New SqlTable
        For Each _DataTable As DataTable In Tables
            Dim _Fields As New DataFields
            For Each _DataColumn As DataColumn In _DataTable.Columns
                _Fields.AddField(_DataColumn.ColumnName, _DataColumn.DataType.ToString, _DataTable.Columns.IndexOf(_DataColumn.ColumnName))
            Next
            OneSqlTable.DataFields.Add(_Fields)
        Next


    End Sub
End Class

在第OneSqlTable.DataFields.Add(_Fields)行的子LoadData中,我收到错误,没有设置DataFields。我想,我已经用Dim OneSqlTable As New SqlTable设置了它。

你能告诉我,我错在哪里吗?

感谢。

1 个答案:

答案 0 :(得分:0)

Dim OneSqlTable As New SqlTable创建一个新的SqlTable对象,但该对象具有null DataFields属性。

你需要像这样定义SqlTable:

Public Structure SqlTable
    Dim DataFields As List(Of DataFields)
    Dim Rows As List(Of DataRow)

    Public Sub New()
       DataFields = New List(Of DataFields)
       Rows = New List(Of DataRow)
    End Sub
End Structure