c# - String returning with a null reference value -


i have silly error can't shot of.

i'm using string check name of folders , extension types. i'm doing can navigate between folders. works fine first folder selection, however, when try , click on second 1 null reference exception.

this have @ moment.

 string clickobject = listbox1.selecteditem.tostring();  int index = clickobject .lastindexof('.');  string extension = clickobject .substring(index + 1, clickobject .length - index - 1);   if (extension == "folder")   {       // stuff    } 

after check files in folder.

when go root of searchable folder , click on directory, when error , line string clickobject = listbox1.selecteditem.tostring(); highlighted.

at end of method set tried setting clickedobject = null; tried remove string contained clickobject.remove(0); error still persists.

how can clear information held in clickedobject can overwrite new information?

edit

sorry forgot mention when go root have button calls method:

 using (var client = new webclient())         {             result = client.downloadstring("http://foo.foo.com/images/getdirectorylist.php");         }          string[] names = result.split('|');         listbox1.items.clear();         foreach (string name in names)         {             listbox1.items.add(name);         }         listbox1.update();          listbox1.selectedindexchanged += new system.eventhandler(this.listbox1_selectedindexchanged); 

however, when click item in list box, first set of code gets used , in sepearate method.

if handling selectedindexchanged event need cope being null there cases won't set.

therefore in handler must check listbox1.selecteditem not null before trying convert string:

if (listbox1.selecteditem != null) {     // code } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -