1

Im working on a app for my study. Now i just started a app where i got a database with the soccer league's and the clubs etc. My app has the following features

  • Combobox with the league's.
  • Listbox that shows the clubs based on your selection from the league.
  • Listbox that show the players based on your selection from the clubs. Now i get this error

    System.NullReferenceException: object reference not set to an instance of an object

Now i know why this error is caused because if i select a player it gives some information about the player to labels. But if select a other league in the combobox or a other club in the listbox the selection changed in the player listbox which triggers the event and will try to update the labels but there is no player selected so it will show this error.

Now to fix this error i have no clue what i can do so it wont trigger the event if i already selected a player and try to change club or league.

How the labels are being updated:

private void Listboxspelers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //set player statistics
    lblpositie.Content = "Positie: " + db.GetPositie(listboxspelers.SelectedItem.ToString());
    lbldoelpunten.Content = "Aantal Doelpunten: " + db.GetDoelpunten(listboxspelers.SelectedItem.ToString());
    lblgelekaarten.Content = "Aantal GeleKaarten: " + db.GetGeleKaarten(listboxspelers.SelectedItem.ToString());
    lblRodeKaarten.Content = "Aantal RodeKaarten: " + db.GetRodeKaarten(listboxspelers.SelectedItem.ToString());
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
Jdiehl
  • 201
  • 3
  • 14

2 Answers2

3

Check that listboxspelers.SelectedItem is not null before you call ToString(). You should also call the ToString() method just once since you pass the same value to all methods:

private void Listboxspelers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //set player statistics
    if (listboxspelers != null && listboxspelers.SelectedItem != null)
    {
        string selectedItem = listboxspelers.SelectedItem.ToString();
        lblpositie.Content = "Positie: " + db.GetPositie(selectedItem);
        lbldoelpunten.Content = "Aantal Doelpunten: " + db.GetDoelpunten(selectedItem);
        lblgelekaarten.Content = "Aantal GeleKaarten: " + db.GetGeleKaarten(selectedItem);
        lblRodeKaarten.Content = "Aantal RodeKaarten: " + db.GetRodeKaarten(selectedItem);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
0

Try this:

private void Listboxspelers_SelectionChanged(object sender, SelectionChangedEventArgs e){
    if(listboxspelers.SelectedItem != null){
        lblpositie.Content = "Positie: " + db.GetPositie(listboxspelers.SelectedItem.ToString());
        lbldoelpunten.Content = "Aantal Doelpunten: " + db.GetDoelpunten(listboxspelers.SelectedItem.ToString());
        lblgelekaarten.Content = "Aantal GeleKaarten: " + db.GetGeleKaarten(listboxspelers.SelectedItem.ToString());
        lblRodeKaarten.Content = "Aantal RodeKaarten: " + db.GetRodeKaarten(listboxspelers.SelectedItem.ToString());
    }
}
Serhat Oz
  • 788
  • 8
  • 12