Let's assume
listbox1
is not null. So, the problem happens because none of the list box items is selected; in this case
listbox1.SelectedItem
is null; you are trying to dereference null reference by using
ToString
, which gives you this exception.
Please always run your code under debugger before asking questions like this one. If you did, you would see the problem immediately. Generally, always use the debugger every time you have a slightest concern about your run-time behavior.
[EDIT]
Please see my past answer where I complied several past answers on related problems, with source code samples applicable to
ComboBox
and
ListBox
:
combobox.selectedvalue shows {} in winform[
^].
—SA