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

时间:2015-01-23 02:51:56

标签: vb.net nullreferenceexception

我正在制作一个有趣的小游戏,因为它涉及可定制的计算机部件。我为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

1 个答案:

答案 0 :(得分:0)

问题是你没有创建CPU类的实例,你需要New来做到这一点。这是一种方法。在您的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