Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I have a problem about listBoxes. In my project there are two listBoxes. I do update, add,delete,clean operations on these ListBoxes. There is no problem with one of them but the other gives,
An unhandled exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll
Additional information: Object reference not set to an instance of an object

error, with same codes.

For example when i want to clear all items in listBox, i do ;

lboxCity.Items.Clear(); and it gives the error mentioned above.
When i debug the code i recognize that after the code lboxCity.Items.Clear();
compiler goes
private void lboxCity_SelectionChanged(object sender, SelectionChangedEventArgs e){}
an do the operations inside.

Also I tried the same debug in running listBox.
After the code
lboxRegion.Items.Clear(); compiler does not run the function
private void lboxRegion_SelectionChanged(object sender, SelectionChangedEventArgs e)

These are just my observation, i am not sure where is the problem.

If it is not clear please note

Summary, while operating listbox operations, it gives nullReferencesException.
Posted
Comments
Alan N 12-Nov-14 6:30am    
It should be straightforward to find out which object is null using the debugger. My suspicion is that it will be something in the SelectionChanged event handler. When all items are removed from the list box the SelectionChanged event will be raised to indicate that the selection has changed from something to nothing and listbox.SelectedItem will be null.
denegabze 12-Nov-14 8:19am    
Thanks Alan, i am tring to fing out where the listbox is assigned to null. I check already but i will continue to check
denegabze 12-Nov-14 11:22am    
i have not managed to solve the issue. I am sure while i am doing for example an assignmet to listbox or a clean operation, listBox is fully filled, but still it give nullreferencesexception

For the lboxCity.Items.Clear(); method to fail with a null reference error means lboxCity was not created, or you have done something to delete the Control, or you have assigned something else, like 'null, to the name 'lboxCity.

Put a break-point in your code on the line where you execute lboxCity.Items.Clear(); and when the application stops there, examine the value of lboxCity by hovering the mouse over it.

The second error: once you have cleared the Items collection of a ListBox why should the SelectionChanged Event ever fire: there's nothing there to select.
 
Share this answer
 
Comments
denegabze 12-Nov-14 4:44am    
lBoxCity is filled when the clear attribute is runned.

And i dont know why the compiler goes SelectionChanged Event after running lBoxCity.Items.Clear function. I just followed the code by debugging and observe that compiler is going SelectionChanged Event.

Also i fotget mention, i use wpf and lBoxCity is created in wpf by code
"ListBox Grid.Column="1" Height="484" HorizontalAlignment="Left" Margin="9,36,0,0" Name="lboxCity" VerticalAlignment="Top" Width="234" SelectionChanged="lboxCity_SelectionChanged" "

Thanks a lot.
denegabze 12-Nov-14 4:52am    
I check the code again, but i still can not see any null assignment or deleting lBoxCity.
Hi there,

Is not there anyone that can overcome the problem. I am very confused. While the code
lboxCity.Items.Clear(); is executed system give nullreferencesexception.
and please note that, just before executing the the code lBoxCity is filled.

Thanks
 
Share this answer
 
v2
Comments
BillWoodruff 13-Nov-14 8:33am    
Please post edits or additional information about your question in your original post, rather than posting them as solutions. Also, if you're question is about WPF, the please tag it.
denegabze 13-Nov-14 10:23am    
haha:) sorry i am not experienced about asking questions.
I have solved the issue.
The problem is, after the code lboxCity.Items.Clear() executed compiler goes to
lboxRegion_SelectionChanged function and takes SelectedItem as null. So it gives nullreferencesexception.
I added a contol to lboxRegion_SelectionChanged and the problem is solved

Thanks for everything

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900