首页 > 解决方案 > VB.net 类对象引用未设置为对象的实例

问题描述

我知道这个问题之前已经被问过很多次了,但是在阅读了所有的回复之后我就是无法解决。我不知道我做错了什么,现在看了 2 天。我有一个嵌套类,只是试图为其分配值,但我不断收到此错误。调试并没有让我更进一步。

我的班级定义:

Public Class DocumentHeader
    Private _username As String
    <JsonProperty("username")> _
    Public Property username() As String
        Get
            Return _username
        End Get
        Set(ByVal value As String)
            _username = value
        End Set
    End Property

    Private _bus_act As String
    <JsonProperty("bus-act")> _
    Public Property bus_act() As String
        Get
            Return _bus_act
        End Get
        Set(ByVal value As String)
            _bus_act = value
        End Set
    End Property

    Private _ref_doc_no As String
    <JsonProperty("ref-doc-no")> _
    Public Property ref_doc_no() As String
        Get
            Return _ref_doc_no
        End Get
        Set(ByVal value As String)
            _ref_doc_no = value
        End Set
    End Property

    Private _header_txt As String
    <JsonProperty("header-txt")> _
    Public Property header_txt() As String
        Get
            Return _header_txt
        End Get
        Set(ByVal value As String)
            _header_txt = value
        End Set
    End Property

    Private _comp_code As String
    <JsonProperty("comp-code")> _
    Public Property comp_code() As String
        Get
            Return _comp_code
        End Get
        Set(ByVal value As String)
            _comp_code = value
        End Set
    End Property

    Private _pstng_date As String
    <JsonProperty("pstng-date")> _
    Public Property pstng_date() As String
        Get
            Return _pstng_date
        End Get
        Set(ByVal value As String)
            _pstng_date = value
        End Set
    End Property

    Private _trans_date As String
    <JsonProperty("trans-date")> _
    Public Property trans_date() As String
        Get
            Return _trans_date
        End Get
        Set(ByVal value As String)
            _trans_date = value
        End Set
    End Property

    Private _fis_period As String
    <JsonProperty("fis-period")> _
    Public Property fis_period() As String
        Get
            Return _fis_period
        End Get
        Set(ByVal value As String)
            _fis_period = value
        End Set
    End Property

    Private _doc_date As String
    <JsonProperty("doc-date")> _
    Public Property doc_date() As String
        Get
            Return _doc_date
        End Get
        Set(ByVal value As String)
            _doc_date = value
        End Set
    End Property

    Private _doc_type As String
    <JsonProperty("doc-type")> _
    Public Property doc_type() As String
        Get
            Return _doc_type
        End Get
        Set(ByVal value As String)
            _doc_type = value
        End Set
    End Property
End Class

这是我的主要课程:

Public Class RootObjectInvoice

    Private _document_header As DocumentHeader
    Public Property document_header() As DocumentHeader
        Get
            Return _document_header
        End Get
        Set(ByVal value As DocumentHeader)
            _document_header = value
        End Set
    End Property
End Class

到目前为止,一切都很好,我想。
现在是我的代码:

Dim Root As RootObjectInvoice
Dim Document_Header As DocumentHeader
Root = New RootObjectInvoice
Document_Header = New DocumentHeader
Try
    Document_Header = Root.document_header
    Document_Header.bus_act = "AAAA"

在这条线上Document_Header.bus_act = "AAAA",我得到了NullpointerException. 我在这里遗漏了有关如何初始化对象的内容。
任何输入将不胜感激。

由于现在可以使用,因此我有以下问题(我习惯于 C#,而不是 VB)我将此属性添加到 RootObjectInvoice

Private _metadata As List(Of Metadata)
Public Property metadata() As List(Of Metadata)
    Get
        Return _metadata
    End Get
    Set(ByVal value As List(Of Metadata))
        _metadata = value
    End Set
End Property

然后尝试初始化它:

Dim Meta_Data() As List(Of Metadata)
Root.metadata() = New List(Of Metadata)
Meta_Data = Root.metadata

但最后一行给了我以下错误:

“System.Collections.Generic.List(Of Metadata)”类型的值无法转换为“System.Collections.Generic.List(Of Metadata)”的一维数组。

标签: vb.netclassjson.netnullreferenceexception

解决方案


Surely this line:

Document_Header = Root.document_header

should be the other way around, i.e.

Root.document_header = Document_Header

By the way, there's no point providing a full implementation of a property like this:

Private _document_header As DocumentHeader

Public Property document_header() As DocumentHeader
    Get
        Return _document_header
    End Get
    Set(ByVal value As DocumentHeader)
        _document_header = value
    End Set
End Property

when you can just do this:

Public Property document_header() As DocumentHeader

and achieve the same thing. You should only provide a full implementation if you need to do something other than get and set a backing field, e.g. validate a new value or raise an event.


推荐阅读