Vb.net 尝试将对象变量设置为标签文本并获取NullReferenceException

Vb.net 尝试将对象变量设置为标签文本并获取NullReferenceException,vb.net,nullreferenceexception,Vb.net,Nullreferenceexception,我正在做一个有趣的小游戏,因为它涉及可定制的计算机部件。我为CPU创建了一个类,并包含了构建CPU对象所需的变量。我有一个函数,它接受构造的对象(如Intel i5-4770)并为它们赋值。我有一个小商店,你可以在那里购买新的硬件,我正试图设置一个标签的对象的名称,使其标题你所购买的。示例:Label1.text=i5_4770.name,但出现以下错误: 其他信息:对象引用未设置为对象的实例 它不能为NULL,因为我已经给了它一个值 这是我的密码: Public Class Cracking4

我正在做一个有趣的小游戏,因为它涉及可定制的计算机部件。我为CPU创建了一个类,并包含了构建CPU对象所需的变量。我有一个函数,它接受构造的对象(如Intel i5-4770)并为它们赋值。我有一个小商店,你可以在那里购买新的硬件,我正试图设置一个标签的对象的名称,使其标题你所购买的。示例:Label1.text=i5_4770.name,但出现以下错误:

其他信息:对象引用未设置为对象的实例

它不能为NULL,因为我已经给了它一个值

这是我的密码:

Public Class Cracking4CashMain

    'Instantiate Objects
    Public Celeron_E3400 As CPU
    Public Pentium_D830 As CPU
    Public Pentium4 As CPU
    Public AMD_A65400 As CPU
    Public Celeron_G1840 As CPU
    Public AMD_A66400 As CPU
    Public Pentium_G3258 As CPU
    Public AMD_FX4300 As CPU
    Public Pentium_G3450 As CPU
    Public AMD_A106800 As CPU
    Public i5_4430 As CPU
    Public i7_4770 As CPU
    Public i7_5930k As CPU
    Public i7_5960x As CPU

    Private Sub Cracking4CashMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Momsbasement.Visible = True
        MomsRoom.Visible = False
        YourHouse.Visible = False
        officeBuilding.Visible = False
        ovalOffice.Visible = False
        'initializeObjects()
        backgroundCheck()
        shopInfo()

    End Sub

    Private Sub initializeObjects()
        Celeron_E3400.setValues(2.6, 1, "Celeron E3400 Processor", 1, 0, 1, 0, 16.99)
        Pentium_D830.setValues(3.0, 2, "Intel Pentium D 830 Smithfield Dual Core", 1, 0, 2, 0, 17.99)
        Pentium4.setValues(2.93, 1, "Intel Pentium 4 521 Prescott", 1, 0, 1, 0, 19.99)
        AMD_A65400.setValues(3.6, 2, "AMD A6-5400k Trinity Dual-Core", 1, 0, 1, 0, 50.0)
        Celeron_G1840.setValues(2.8, 2, "Intel Celeron G1840 Haswell Dual-Core", 0.256, 2, 2, 1, 46.99)
        AMD_A66400.setValues(3.9, 2, "AMD A6-6400k Richland Dual-Core", 1, 0, 1, 0, 62.99)
        Pentium_G3258.setValues(3.2, 2, "Intel Pentium G3258 Haswell Dual-Core", 0, 3, 0, 3, 69.99)
        AMD_FX4300.setValues(3.8, 4, "AMD FX-4300 Vishera Quad-Core", 2, 4, 2, 1, 99.99)
        Pentium_G3450.setValues(3.4, 2, "Intel Pentium G3450 Haswell Dual Core", 0.256, 3, 2, 1, 86.99)
        AMD_A106800.setValues(4.1, 4, "AMD A10-6800k Richland", 4, 0, 1, 0, 129.99)
        i5_4430.setValues(3.0, 4, "Intel Core i5-4430 Haswell Quad-Core", 0, 6, 0, 1, 184.99)
        i7_4770.setValues(3.4, 4, "Intel Core i7-4770 Haswell Quad-Core", 0, 8, 0, 1, 309.99)
        i7_5930k.setValues(3.5, 6, "Intel Core i7-5930k Haswell-E 6-Core", 0.256, 15, 6, 1, 564.99)
        i7_5960x.setValues(3.0, 8, "Intel Core i7-5960x Haswell-E 8-Core", 0.256, 20, 8, 1, 1049.99)
    End Sub
    Private Sub backgroundCheck()
        If (Momsbasement.Visible = True) Then
            monitor.Parent = Momsbasement
            PictureBox1.Parent = Momsbasement
        ElseIf (MomsRoom.Visible = True) Then
            monitor.Parent = MomsRoom
            PictureBox1.Parent = MomsRoom
        ElseIf (YourHouse.Visible = True) Then
            monitor.Parent = YourHouse
            PictureBox1.Parent = YourHouse
        ElseIf (officeBuilding.Visible = True) Then
            monitor.Parent = officeBuilding
            PictureBox1.Parent = officeBuilding
        ElseIf (ovalOffice.Visible = True) Then
            monitor.Parent = ovalOffice
            PictureBox1.Parent = ovalOffice
        End If
    End Sub

    Private Sub Cracking4CashMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Cracking4CashLogin.Close()
    End Sub

    Private Sub monitor_Click(sender As Object, e As EventArgs) Handles monitor.Click

    End Sub

    Private Sub shopInfo()
        Label1.Text = i5_4430.name
    End Sub
End Class
类别:

Public Class CPU

    Public price As Double
    Public GHz As Double
    Public cores As Integer
    Public L2Cache As Double
    Public L3Cache As Double
    Public numL2 As Integer
    Public numL3 As Integer
    Public name As String

    Public Sub setValues(ByVal GH As Double, ByVal core As Integer, ByVal n As String, ByVal l2 As Double, ByVal l3 As Double, ByVal nl2 As Integer, ByVal nl3 As Integer, ByVal p As Double)
        GHz = GH
        cores = core
        L2Cache = l2
        L3Cache = l3
        numL2 = nl2
        numL3 = nl3
        price = p
        name = n
    End Sub
End Class

问题是您没有创建CPU类的实例,您需要新的实例来创建。这里有一种方法可以做到这一点。在CPU类中,将setValues方法的名称更改为New

Public Sub New(GH As Double, core As Integer, n As String, l2 As Double, _
    l3 As Double, nl2 As Integer, nl3 As Integer, p As Double)
    'The rest of your setValues code goes here
End Sub
现在,您可以更改initializeObjects方法来创建新的CPU对象,同时设置值

Private Sub initializeObjects()
    Celeron_E3400 = New CPU(2.6, 1, "Celeron E3400 Processor", 1, 0, 1, 0, 16.99)
    'etc.
End Sub

公共i5_4430作为新的CPU将出现堆栈溢出!几乎所有NullReferenceException的情况都是相同的。请参阅以获得一些提示。无论何时出现该错误,请检查引发异常的行。其中一个对象什么都不是,找到该对象是第一步。它通常也是你唯一真正需要的,但它是必不可少的。那么,异常发生在哪一行?每当你发布一个异常时,它是一个异常,而不是一个错误,请发布完整的异常,包括堆栈跟踪。我看到很多私有子。但是调用导致NullReferenceException的代码的代码在哪里?我看不出有任何方法可以调用这些代码。什么是MyBase的基类?@JohnSaunders:我同意,但OP对.net来说似乎相对较新。我不确定他是否能够从非常全面的解释和C代码中了解到他需要做什么来纠正他的VB代码。那里还有VB.NET示例,除了代码示例之外,还有很多文本需要阅读。