question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ListView.Items.RemoveAt causes NullReferenceException

See original GitHub issue
  • .NET Core Version: 6.0.0

  • Have you experienced this same bug with .NET Framework?: Classic .NET - No .NET 5 - No .NET 6 - Yes

Problem description: Deleting a selected item from ListView causes NullReferenceException on calling ListView.Items.RemoveAt(index). The bug reproduces stable in my app in the following conditions:

  1. The item being deleted must be selected. If it’s not selected, no error.
  2. To workaround the error, we can call ListView.Focus() before delete.
  3. ListView is a Details view.

The code that fails: `

// listView1.Focus(); // helps to avoid error

listView1.Items.RemoveAt(0); // error is here if item 0 is selected

MessageBox.Show("a"); // shows after error

`

Despite the error, the item is deleted.

Error stack: System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.ListViewItem.get_AccessibilityObject() at System.Windows.Forms.ListView.ListViewAccessibleObject.GetFocus() at System.Windows.Forms.ListView.OnGotFocus(EventArgs e) at System.Windows.Forms.Control.WmSetFocus(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ListView.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

Expected behavior: No error in the above case. Because it worked well before .NET 6.

Minimal repro: I couldn’t create a small app to reproduce the error, because there is no error in a simple app. However, I hope that a developer who works daily on WinForms, can find the possible mistake in ListViewItem.get_AccessibilityObject() Edit: the test app is attached below.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RussKiecommented, Nov 25, 2021
0reactions
msftbot[bot]commented, Mar 8, 2022

Hi @MelonWang1, it looks like you just commented on a closed issue. The team will most probably miss it. If you have a question - consider opening a new discussion thread. Alternatively, you’d like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullReferenceException, ListView Object reference not set ...
System. NullReferenceException was unhandled by user code HResult=-2147467261 Message=Object reference not set to an instance of an object. ...
Read more >
[Bug] System.NullReferenceException when ListView ...
Temporary solution for this is to set the ItemSource to null, updating the List and setting the ItemSource back to the List. Steps...
Read more >
[Android] null items in ListView ItemsSource causes ...
When an item in the ItemsSource of a ListView is null, a NullReferenceException will be raised on Android when the items source is...
Read more >
In iOS, calling .Clear() on an ObservableCollection bound ...
Description I receive the following exception which appears to be thrown from the iOS internals (Android is fine) when clearing a listview's ......
Read more >
Listview Model Binding - NullReferenceException
Hi, I'm trying to have my ListView bind to a model item, so that when postback occurs, the entries updated to the listview,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found