0

I'm trying to debug a program which uses a ListView to create a list of icons for files stored in a DB. I got the log of an exception stating System.nullreferenceexception: 'object reference not set to an instance of an object.'.

What I get specifically from the log file is this

System.nullreferenceexception: object reference not set to an instance of an object.
in System.Windows.Forms.ListViewItem.set_Selected(Boolean value)
in (MyFilesControl , Boolean ) 
in MyLibrary.MyFilesControl.MyMethod(Boolean MyBooleanVariable)

where MyFilesControl is a custom Control basically used to show a tree view of folders, a file view and other things.

The real problem is that last line in the log seems to imply the error is generated here.

If MyBooleanVariable AndAlso MyListView.Items.Count > 0 AndAlso MyListView.SelectedItems.Count = 0 Then
    MyListView.Items(0).Selected = True ' error here, most likely
End If

This implies in that last line of code something should be Nothing... Except in theory it CAN'T be nothing. Right? All 3 conditions right above exist (included MyBooleanVariable, since it's an optional Boolean created and passed by MyMethod).

So my main issue and doubt is: can something be Nothing in that last line of code? I also tried to set the value to Nothing but I get the exception on the new added line, that's why I'm assuming a ListItem cannot be Nothing. Asking also because I'm trying to replicate the exception but I seem uncapable of ever getting it. The code always works for me but still that log exists.

6
  • 1
    There can be a NRE in the .NET code of the set accessor of the Selected property. That's not a simple property but this. It could be for example a threading issue. Commented Apr 18, 2024 at 15:05
  • 1
    "error here, most likely"?? Set a breakpoint and inspect the variables, properties, etc.. Then you will know exactly where. Tutorial: Learn to debug Visual Basic code using Visual Studio. Commented Apr 18, 2024 at 15:11
  • @Olivier no, that's the point: i can't ever get the exception so I can't know 100% sure if the error is there. it's the only line that make sense in the code but at the same time i never get the error. I only know by the log that the error can occur. So the main point is: I know that in theory the error is there but how can it be if there can't be a Nothing value? Commented Apr 18, 2024 at 15:14
  • @TimSchmelter actually I don't understand what the code does but as I see it I can in theory take the hypothesis Selected can return an exception... Only I don't understand how and why others would get this error and I don't. It's difficult to debug something you can't replicate or understand... Commented Apr 18, 2024 at 15:22
  • @grimoiredark: we just have to gues since we dont know the full code. Is it possible that the program uses tasks or multiple threads which all access the listview? Commented Apr 18, 2024 at 15:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.