-2
Public Class Form1
Private firstClicked As Label = Nothing
Private secondClicked As Label = Nothing
Private random As New Random
Private icons =
    New List(Of String) From {"!", "!", "N", "N", ",", ",", "k", "k", "b", "b", "v", "v", "w", "w", "z", "z"}
Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    AssignIconsToSquares()
End Sub
Private Sub AssignIconsToSquares()
    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

Private Sub label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click, 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 clickedLabel1 = TryCast(sender, Label)
    If clickedLabel1 IsNot Nothing Then
        If clickedLabel1.ForeColor = Color.Black Then Exit Sub
        If firstClicked Is Nothing Then
            firstClicked = clickedLabel1
            firstClicked.ForeColor = Color.Black
            Timer1.Start()
            Exit Sub
        End If
    End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Timer1.Stop()
    firstClicked.ForeColor = firstClicked.BackColor
    secondClicked.ForeColor = secondClicked.BackColor
    firstClicked = Nothing
    secondClicked = Nothing
End Sub
End Class

Hi, this is the code for a matching game that I am making following a tutorial from here: http://msdn.microsoft.com/en-us/library/vstudio/dd553235(v=vs.100).aspx

My problem is I get the error:

A first chance exception of type 'System.NullReferenceException'
occurred in Matching Game.exe

Which points to the line:

secondClicked.ForeColor = secondClicked.BackColor

Can someone help me out here please?

DreamOfMirrors
  • 2,147
  • 1
  • 21
  • 34
Marcel
  • 149
  • 7
  • 16
  • `firstClicked` is nothing (null) until the label is clicked. if the timer goes off before that (ie it is enabled to start with), you will get a NullReferenceException – Ňɏssa Pøngjǣrdenlarp Jun 04 '14 at 12:40
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Jun 04 '14 at 12:59

1 Answers1

1

You haven't assigned secondClicked instance:

' secondClicked is null (nothing)
Private secondClicked As Label = Nothing;
...

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Timer1.Stop()
    firstClicked.ForeColor = firstClicked.BackColor
    ' Trying to get BackColor and ForeColor of null (nothing)
    secondClicked.ForeColor = secondClicked.BackColor 

Probably (it's difficult to say for sure) you should put it

  firstClicked.ForeColor = firstClicked.BackColor 

  ' if secondClicked can be assigned (not null) change its color
  If secondClicked IsNot Nothing Then
    secondClicked.ForeColor = secondClicked.BackColor

  ' second clicked became first clicked, and first clicked - nothing
  secondClicked = firstClicked
  firstClicked = Nothing
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215