Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting system.NullReferenceException when i am trying to add item in listbox
in a exe which runs on a client server

i add message in listbox like this listboxname.items.add(string message) at that point i am getting error
Posted
Updated 13-Apr-12 2:21am
v2
Comments
CPallini 13-Apr-12 5:47am    
"Not enought information" is what you get from me after asking such a question.
Dipesh202 13-Apr-12 8:22am    
but now i have given much details about error

You call item.ToString(), but item is null. Check that first!
 
Share this answer
 
Comments
Dipesh202 13-Apr-12 8:15am    
but i am using it like that
listboxname.items.add(some string Message)
and it is running in Loop to Print
Message For Each Code
Bernhard Hiller 13-Apr-12 9:47am    
And now show us the code of your "foreach loop". Looks like string message=Messages[code] is null, or something similar.
Hi,

It sounds like you are trying to add a value that has not been assigned yet. As you are running in a loop, make sure you are getting the value you think you are.

If you are using a for loop to read the strings, make sure the instance value you are reading contains what you expect. Example:

C#
for (int i = 0; i <= 5; i++)
{
listboxname.items.add(sourchelist[i]);
}


Make sure that your range is correct... in other words, make sure that if you are creating a 0-base indexed list you start reading from 0, and that you stop reading at the correct point. e.g. make sure your range selector (<= 5 in the above example) is correct. Otherwise you may be trying to read from index 5 when the source list only has 5 entries (in which case you should stop reading at 4).

Make sure you assign the initial value of the string you are adding. i.e. Where you create the variable that contains the text you are adding to the list use:

C#
string textToAdd = "";
// Rest of your code...


in stead of
C#
string textToAdd;
// Rest of your code...



Which makes sure that the initial value is set.

You mentioned in your comment that you are using multi-threading. This adds a few more things to check:

1. Make sure you are not running into a race condition (Reading the value before it is set)
2. Make sure you are updating the listbox values from the same thread as the one on which the listbox is running. I usually create a utility method in that code that you can call from other threads to update the listbox:

C#
private void AddToListBox(string str)
{
    if (InvokeRequired) BeginInvoke(new Action<string>(AddToListBox), str);
    else listboxname.Items.Add(str);
}


3. Make sure that if you are using queued locking to ensure your threaded data integrity, that your locks are not timing out and discarding your updates.

Hope that helps!
 
Share this answer
 
v2
Comments
Dipesh202 14-Apr-12 2:03am    
i am just displaying string message in listbox
like this
string message = "";
listboxname.items.add(message)
but it is in a loop and i have used multithreading in my application
and multiple threads access same listbox
for each company id in a loop

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