0

I'm making a fun little game just because and it involves customizable computer parts. I made a class for the CPU and included the needed variables in order to construct a CPU object. I have a function that takes the constructed objects (such as Intel i5-4770) and assigns values to them. I have a little shop where you can purchase new hardware, I'm trying to set a label to the name of the object so it titles what your buying. Example: Label1.text = i5_4770.name but i get the following error:

Additional information: Object reference not set to an instance of an object.

It cant be NULL because i have given it a value.

here's my code:

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

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
John Saunders
  • 160,644
  • 26
  • 247
  • 397
SpiffyAF
  • 11
  • 3
  • 1
    Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 23 '15 at 03:01
  • 1
    Whenever you get that error, examine the line where the exception was thrown. One of the objects is Nothing, and finding that object is the first step. It is often the only one you really need to take, too, but it is essential. So, which line is the exception happening on? – Jun Rikson Jan 23 '15 at 03:01
  • Whenever you post about an exception (it's an _exception_, not an "error"), please post the full exception, including the stack trace. – John Saunders Jan 23 '15 at 03:21
  • I see a lot of `Private` subs. But where is the code that calls the code that causes the `NullReferenceException`? I don't see any way for any of that code to be called. What is the base class (`MyBase`)? – John Saunders Jan 23 '15 at 03:24

1 Answers1

0

The problem is that you are not creating instances of your CPU class, you need New to do that. Here is one way to do that. In your CPU class, change the name of the setValues method to 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

Now you can change your initializeObjects method to create new CPU objects and set the values at the same time.

Private Sub initializeObjects()
    Celeron_E3400 = New CPU(2.6, 1, "Celeron E3400 Processor", 1, 0, 1, 0, 16.99)
    'etc.
End Sub
Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • @JohnSaunders: I agree, but the OP appears to be relatively new to .net. I'm not sure he would be able to get from that very comprehensive explanation and C# code to what he needs to do to correct his VB code. – Blackwood Jan 23 '15 at 03:35
  • 1
    There are also VB.NET examples there, and a lot of text to read in addition to code examples. – John Saunders Jan 23 '15 at 03:36