Click here to Skip to main content
15,885,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public string[] getStopWords()
   {
      string[] stopWordArray = null;
      int itemCount = lbStopWords.Items.Count;

          for (int i = 0; i < itemCount; i++)
          {
              stopWordArray[i] = (string)lbStopWords.Items[i];
          }


       return stopWordArray;
   }



I wrote above method to get listBox items to array and return array but when using that method I am getting exception NullreferenceExeception was unhandle by user code

I couldn't find a way to go throw it.

Is there any other way to do that kind of method

pls help
Posted
Updated 16-Aug-10 9:00am
v2
Comments
Dalek Dave 16-Aug-10 15:00pm    
Minor Edit for Grammar.

public string[] get()
{
string[] arr = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
arr[i] = listBox1.Items[i].ToString();
}
return arr;
}


try dis if it solve ur purpose!!
 
Share this answer
 
Comments
Kasunmit 16-Aug-10 13:50pm    
thank you very much saloni its working U r doing great help !!! :D
The root cause of your problem is that you are using an array that has not been dimensioned. Try this:

C#
public string[] getStopWords()
{
    string[] stopWordArray = new string[lbStopWords.Items.Count];
    int itemCount = lbStopWords.Items.Count;
    for (int i = 0; i < itemCount; i++)
    {
        stopWordArray[i] = lbStopWords.Items[i].ToString();
    }
    return stopWordArray;
}
 
Share this answer
 
I know you already have your answer, but you could also have used:
C#
public string[] getStopWords()
       {
           string[] stopWordArray = new string[listBox1.Items.Count];
           listBox1.Items.CopyTo(stopWordArray,0);

           return stopWordArray;
       }
 
Share this answer
 
Comments
Kasunmit 16-Aug-10 14:39pm    
thanx AnnieMacD .. in programming do one thing we r having lot of ways ... i ll keep in touch ur answer also most probably it will be help me another time thanx again ..

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