Erreur vb.net nullreferenceexception

Fermé
DRekV Messages postés 2 Date d'inscription dimanche 19 juillet 2020 Statut Membre Dernière intervention 19 juillet 2020 - Modifié le 19 juil. 2020 à 20:57
Whismeril Messages postés 19040 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 9 mai 2024 - 19 juil. 2020 à 23:19
bonjour tout le mondre,
aujourdh'ui, jai créer le jeu puzzle de microsoft en vb.net, le matching game. sauf que quand je fait une paire de deux, sa plante. quelqu'un peu maider? voici le 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
    Dim firstClicked As Label = Nothing

    ' secondClicked points to the second Label control 
    ' that the player clicks
    Dim secondClicked As Label = Nothing


    ' Use this Random object to choose random icons for the squares
    Dim random As New Random

    ' Each of these letters is an interesting icon
    ' in the Webdings font,
    ' and each icon appears twice in this list
    Dim temporaryArray() As String = {"!", "!", "N", "N", ",", ",", "k", "k", _
                                      "b", "b", "v", "v", "w", "w", "z", "z"}
    Dim icons As List(Of String) = temporaryArray.ToList()




    ''' <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 As Label = TryCast(control, Label)
            If iconLabel IsNot Nothing Then
                Dim randomNumber As Integer = random.Next(icons.Count)
                iconLabel.Text = icons.ElementAt(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

    ''' <summary>
    ''' Every label's Click event is handled by this event handler
    ''' </summary>
    ''' <param name="sender">The label that was clicked</param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    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

        ' The timer is only on after two non-matching 
        ' icons have been shown to the player, 
        ' so ignore any clicks if the timer is running
        If (Timer1.Enabled = True) Then
            Return
        End If

        Dim clickedLabel As Label = TryCast(sender, Label)

        If clickedLabel IsNot Nothing Then
            ' If the clicked label is black, the player clicked
            ' an icon that's already been revealed --
            ' ignore the click
            If (clickedLabel.ForeColor = Color.Black) Then
                Return
            End If

            ' If firstClicked is Nothing, this is the first icon 
            ' in the pair that the player clicked, 
            ' so set firstClicked to the label that the player 
            ' clicked, change its color to black, and return
            If (firstClicked Is Nothing) Then
                firstClicked = clickedLabel
                firstClicked.ForeColor = Color.Black
                Return
            End If

            ' If the player gets this far, the timer isn't 
            ' running and firstClicked isn't Nothing, 
            ' so this must be the second icon the player clicked
            ' Set its color to black
            secondClicked = clickedLabel
            secondClicked.ForeColor = Color.Black

            ' If the player gets this far, the player 
            ' clicked two different icons, so start the 
            ' timer (which will wait three quarters of 
            ' a second, and then hide the icons)
            Timer1.Start()

            ' If the player gets this far, the timer isn't 
            ' running and firstClicked isn't Nothing, 
            ' so this must be the second icon the player clicked
            ' Set its color to black
            secondClicked = clickedLabel
            secondClicked.ForeColor = Color.Black

            ' If the player clicked two matching icons, keep them 
            ' black and reset firstClicked and secondClicked 
            ' so the player can click another icon
            If (firstClicked.Text = secondClicked.Text) Then
                firstClicked = Nothing
                secondClicked = Nothing
                Return
            End If

            ' If the player gets this far, the player 
            ' clicked two different icons, so start the 
            ' timer (which will wait three quarters of 
            ' a second, and then hide the icons)
            Timer1.Start()

        End If
    End Sub


    ''' This timer is started when the player clicks 
    ''' two icons that don't match,
    ''' so it counts three quarters of a second 
    ''' and then turns itself off and hides both icons
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Timer1_Tick() 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's the first click
        firstClicked = Nothing
        secondClicked = Nothing

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        
    End Sub

    ''' <summary>
    ''' Check every icon to see if it is matched, by 
    ''' comparing its foreground color to its background color. 
    ''' If all of the icons are matched, the player wins
    ''' </summary>
    Private Sub CheckForWinner()

        ' Go through all of the labels in the TableLayoutPanel, 
        ' checking each one to see if its icon is matched
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel As Label = TryCast(control, Label)
            If iconLabel IsNot Nothing Then
                If (iconLabel.ForeColor = iconLabel.BackColor) Then
                    Return
                End If
            End If
        Next

        ' If the loop didn’t return, it didn't find 
        ' any unmatched icons
        ' That means the user won. Show a message and close the form
        MessageBox.Show("Félicitation! Vous avez gagnée! Toutes les cartes ont été retournée correctement.", "Félicitation!")
        Close()

    End Sub


End Class


le probléme viendrai de

firstClicked.ForeColor = firstClicked.BackColor
        secondClicked.ForeColor = secondClicked.BackColor


dans l'onglet
Private Sub Timer1_Tick() Handles Timer1.Tick


esque il y aurai un moyen dignorer l,erreur?

merci davancce

2 réponses

Whismeril Messages postés 19040 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 9 mai 2024 932
19 juil. 2020 à 21:17
Bonsoir

ignorer l'erreur revient à ignorer la ligne.
A ce compte là, autant la supprimer.....

Sinon, tu déboggues.

Sais tu utiliser les outils de déboggage?
As tu compris le message d'erreur?
Si oui as tu espionné les objets et propriétés de ces objets pour savoir le/la quel(le) génère l'erreur?



0
DRekV Messages postés 2 Date d'inscription dimanche 19 juillet 2020 Statut Membre Dernière intervention 19 juillet 2020
19 juil. 2020 à 21:21
et bien, quand je lance l'exécutable du jeu et que quand je je fait continuer sur l,erreur, le bonne paire reste visible. mais queand j'enléve la ligne, les mauvaise paire reste visible. et quand c'est la fin de la partie, quand tout est bon, sa affiche que le jeu est gagnée. donc si j'ignore le message, le jeu vas ce jouer normalement.
0
Whismeril Messages postés 19040 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 9 mai 2024 932
19 juil. 2020 à 23:19
OK, donc écrire un logiciel qui marche de façon aléatoire plutôt que chercher à le rendre fiable est la solution pour toi?
Ce n'est certainement pas comme ça que tu vas progresser.

Quand tu seras prêt à travailler sérieusement, je t'ai posé 3 questions, qui attendent des réponses précises.....
0