im making a matching game were when you click 1 icon and then another of the same type the 2 icons stay in but whenever I run the program I get a error code im really new to this so probably wont understand some of this the error is "system.nullreferneceexception" and it says object reference not set to an instance of an object. the bit of code it happens on is
the whole code is
help please?
Code:
firstClicked.ForeColor = firstClicked.BackColor
Code:
Public Class Form1
' firstClicked points to the first Label control
' that the player clicks, but it will be Nothing
' if the player hasn't clicked a label yet
Private firstClicked As Label = Nothing
' secondClicked points to the second Label control
' that the player clicks
Private secondClicked As Label = Nothing
' Use this Random object to choose random icons for the squares
Private random As New Random
' Each of these letters is an interesting icon
' in the Webdings font,
' and each icon appears twice in this list
Private icons =
New List(Of String) From {"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"}
''' <summary>
''' Assign each icon from the list of icons to a random square
''' </summary>
''' <remarks></remarks>
Private Sub AssignIconsToSquares()
' The TableLayoutPanel has 16 labels,
' and the icon list has 16 icons,
' so an icon is pulled at random from the list
' and added to each label
For Each control In TableLayoutPanel1.Controls
Dim iconLabel = TryCast(control, Label)
If iconLabel IsNot Nothing Then
Dim randomNumber = random.Next(icons.Count)
iconLabel.Text = icons(randomNumber)
iconLabel.ForeColor = iconLabel.BackColor
icons.RemoveAt(randomNumber)
End If
Next
End Sub
Public Sub New()
' This call is required by Windows Form Designer
InitializeComponent()
' Add any initialization after the InitializeComponent() call
AssignIconsToSquares()
End Sub
Private Sub label_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click
If Timer1.Enabled Then Exit Sub
Dim clickedlabel = TryCast(sender, Label)
If clickedlabel IsNot Nothing Then
If clickedlabel.ForeColor = Color.Black Then Exit Sub
If firstClicked Is Nothing Then
firstClicked = clickedlabel
firstClicked.ForeColor = Color.Black
Exit Sub
End If
secondClicked = clickedlabel
secondClicked.ForeColor = Color.Black
If firstClicked.Text = secondClicked.Text Then
firstClicked = Nothing
secondClicked = Nothing
End If
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'stop the timer
Timer1.Stop()
'hide both icons
firstClicked.ForeColor = firstClicked.BackColor
secondClicked.ForeColor = secondClicked.BackColor
'reset firstclicked and secondclicked
'so the next time a label is
'clicked, the program knows it is firstclicked
firstClicked = Nothing
secondClicked = Nothing
End Sub
End Class