gogo199432
gogo199432

Reputation: 35

WPF ListView throws exception whenever I change items in it

So I have been having this problem for several days now, where I have a simple ListView item that gets filled up dynamically. The user can add to a list entries which should show up on the display. This works, however when I try to do anything with the listView Items, either delete or even refresh them, I get a NullReferenceException. Please take a look at the code below.

The XAML file (snippet):

<ListView x:Name="listView" Grid.Column="1" HorizontalAlignment="Left" Height="100" Margin="10,97,0,0" Grid.Row="1" VerticalAlignment="Top" Width="112" IsSynchronizedWithCurrentItem="False">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Names" DisplayMemberBinding="{Binding documentName}"/>
        </GridView>
    </ListView.View>
</ListView>

And here is a snippet from the .cs code:

public class Entry {
  public string documentName {
    get;
    set;
  }

  public string languageAssistantName {
    get;
    set;
  }

  public string assignmentDate {
    get;
    set;
  }

  public string deadline {
    get;
    set;
  }

  public string progress {
    get;
    set;
  }

  public string supervisorName {
    get;
    set;
  }

  public string remarks {
    get;
    set;
  }

  public string proofread {
    get;
    set;
  }

  public Entry(string documentName, string assignmentDate, string deadline, string supervisorName, string remarks, string LAName, string progress, string proofread) {
    this.documentName = documentName;
    this.assignmentDate = assignmentDate;
    this.deadline = deadline;
    this.supervisorName = supervisorName;
    this.remarks = remarks;
    this.languageAssistantName = LAName;
    this.progress = progress;
    this.proofread = proofread;
  }
}

List < Entry > entryList = new List < Entry > ();

private void UpdateListView() //This is the code that adds the new entries to the listView, this works
{
  listLabel.Text = "";
  for (int i = 0; i < entryList.Count; i++) {
    listLabel.Text += i + " | " + entryList[i].ToString() + "\n\n";
    listView.Items.Insert(0, entryList[i]);
  }
}

/*This is the part that throws exception

*/
private void button_Click(object sender, RoutedEventArgs e) {
  listView.Items.RemoveAt(0);
}

I have tried this with SelectedItem, Refresh, ItemsSource and everything else I could think of :/ . Every time I try to remove anything from the list, I get NullReferenceException. And the worst is that it doesn't actually show me a line where the error occurs, so I'm assuming it's something out of my control. But when I tried to fix .Net framework, it said nothing was wrong. Any ideas?

Upvotes: 0

Views: 455

Answers (1)

gogo199432
gogo199432

Reputation: 35

Okay, so apparently I was an idiot. I didn't know that Visual Studio actually refuses to show the location of the error if I'm launching my project in 'Release' instead of 'Debug'. So after switching that back, I found out that the error was completely unrelated to listView.

Apparently in the 'Equal' operator of the Entry class, the p object was sometimes null. Now I still don't know where that comes from, but after adding an extra if statement, everything works wonderfully.

Thanks for the help anyway :)

Upvotes: 1

Related Questions