格式化时,对象引用未设置为对象的实例

时间:2014-12-27 19:46:33

标签: vb.net visual-studio-2012 formatting nullreferenceexception

当我尝试启动应用程序时,我在Visual Studio中收到错误“对象引用未设置为对象的实例”。我正在使用Visual Basic

这是发生错误的地方:

Public Overrides Function ToString() As String
    Dim strOut As String = String.Format("{0, -20} {1}", LastName, m_address.ToString())
    Return strOut
End Function

m_address.ToString()是:(以下代码没有错误)

Public Overrides Function ToString() As String
    Dim strOut As String = String.Format("{0, -20}{1,-10}{2, -10}{3, -10}",
                                         m_street, m_zipCode, m_city, GetCountryString())
    Return strOut
End Function

有谁知道如何修复错误?谢谢!

更新:

Private m_address As Address

'Creates the m_address object in the constructor
Public Sub New()
    m_address = New Address()
End Sub

更新V2:

Public Class Address

    Private m_street As String
    Private m_zipCode As String
    Private m_city As String
    Private m_country As Countries

    'Defualt constructor
    Public Sub New()

        Me.New(String.Empty, String.Empty, "Malmö")
    End Sub

    'Constructors
    'Constructors calling another constructor
    Public Sub New(ByVal steet As String,
                   ByVal zip As String,
                   ByVal city As String,
                   ByVal country As Countries)
        Me.m_street = Street
        Me.m_zipCode = zip
        Me.m_city = city
        Me.m_country = country
    End Sub

    Public Sub New(ByVal street As String, ByVal zip As String, ByVal city As String)
        Me.m_street = street
        Me.m_zipCode = zip
        Me.m_city = city
    End Sub

    Public Sub New(ByVal theOther As Address)
        Me.m_street = theOther.Street
        Me.ZipCode = theOther.ZipCode
        Me.m_city = theOther.City
    End Sub
'Propoties..

Public Function GetCountryString() As String
    Dim strCountry As String = m_country.ToString()
    strCountry = strCountry.Replace("_", " ")
    Return strCountry
End Function

更新V3

'Here i list all the countries in the world
        Public Enum Countries
    Country 1
    Country 2
    Country 3 
    Country 4
    etc..
    End Enum

1 个答案:

答案 0 :(得分:0)

请务必初始化所有已使用的变量或:

更改

Public Overrides Function ToString() As String
   Dim strOut As String = String.Format("{0, -20} {1}", LastName, m_address.ToString())
   Return strOut
End Function

Public Overrides Function ToString() As String
   Dim strOut As String = String.Format("{0, -20} {1}", LastName, m_address)
   Return strOut
End Function

也改变了

Public Function GetCountryString() As String
    Dim strCountry As String = m_country.ToString()
    strCountry = strCountry.Replace("_", " ")
    Return strCountry
End Function

Public Function GetCountryString() As String
    If m_country Is Nothing Then Return Nothing
    Dim strCountry As String = m_country.ToString()
    strCountry = strCountry.Replace("_", " ")
    Return strCountry
End Function

未初始化对象上的ToString()将因您收到的错误而失败。

使用String.Format()时,不必对要添加的每个变量使用.ToString()。这会自动发生,如果对象为null,则“对象引用未设置为对象的实例”不会失败。它将默认为String.Empty。