?
Solved

Listbox items into strings

Posted on 2005-03-10
11
Medium Priority
?
187 Views
Last Modified: 2010-04-24
Hello,

Here's what i think is right:
String *items[];
len = listBox1->Items->Count;
for(Int32 i=0;i!=len;i++){
   items[i] = listBox1->Items->Item[i]->ToString();
}

It compiles right, but I get an error when I run it:
Object reference not set to an instance of an object.

Thanks for any help.
0
Comment
Question by:Mad_Angel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 18

Expert Comment

by:testn
ID: 13513440
possible that your item may be nulll

 items[i] = System::Convert::ToString(this->listBox1->Items->Item[i]);
0
 

Author Comment

by:Mad_Angel
ID: 13513487
Nope, still same error.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at MoneyTreeGrabber.Form1.button4_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Here's my code:

private: System::Void button4_Click(System::Object *  sender, System::EventArgs *  e)
             {
                   if(radioButton1->Checked == true){
                         listBox1->Enabled = false;
                         button3->Enabled = false;
                         button4->Enabled = false;
                         button5->Enabled = true;
                         button6->Enabled = false;
                         check = 1;
                         timer1->Stop();
                         timer1->Start();
                   }
                   else if(radioButton2->Checked == true){
                         if(listBox1->Items->Count < 1){
                               MessageBox::Show(S"You need to enter at least 1 item", S"Warning");
                         }
                         else{
                               len = listBox1->Items->Count;
                               for(Int32 i=0;i!=len;i++){
                                     items[i] = System::Convert::ToString(this->listBox1->Items->Item[i]);
                               }
                               listBox1->Enabled = false;
                               button3->Enabled = false;
                               button4->Enabled = false;
                               button5->Enabled = true;
                               button6->Enabled = false;
                               check = 2;
                               timer1->Stop();
                               timer1->Start();
                         }
                   }
                   else if(radioButton3->Checked == true){
                         if(listBox1->Items->Count < 1){
                               MessageBox::Show(S"You need to enter at least 1");
                         }
                         else{
                               len = listBox1->Items->Count;
                               for(Int32 i=0;i!=len;i++){
                                     items[i] = System::Convert::ToString(this->listBox1->Items->Item[i]);
                                     MessageBox::Show(items[i], S"Items");
                               }
                               listBox1->Enabled = false;
                               button3->Enabled = false;
                               button4->Enabled = false;
                               button5->Enabled = true;
                               button6->Enabled = false;
                               check = 3;
                               timer1->Stop();
                               timer1->Start();
                         }
                   }
             }
0
 
LVL 18

Expert Comment

by:testn
ID: 13513542
you forgot to initialize items
0
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
LVL 18

Expert Comment

by:testn
ID: 13513544
String *items[] = new String[len];
0
 

Author Comment

by:Mad_Angel
ID: 13513550
it is initialized globally. if i forgot to initialize i think it would not compile. I also made sure that the listbox contains at least one item.
0
 

Author Comment

by:Mad_Angel
ID: 13513554
How do i make that globally?
0
 

Author Comment

by:Mad_Angel
ID: 13513565
String *items[] = new String[len]; gives out an error:
error C2691: 'System::String' : invalid type for __gc array element
error C3149: 'System::String' : illegal use of managed type 'System::String'; did you forget a '*'?
error C2691: 'System::String __gc *' : invalid type for __gc array element
0
 
LVL 18

Expert Comment

by:testn
ID: 13513677
it should
///// declare this globally
 String* items[];


/////// put this in your method
items[] = new String*[len];
0
 

Author Comment

by:Mad_Angel
ID: 13513693
Ok, now this: items[] = new String*[len]; gives me this error message:
error C2059: syntax error : ']'

Thanks again.
0
 
LVL 18

Accepted Solution

by:
testn earned 2000 total points
ID: 13513767
sorry remove [] i forgot....

items = new String*[len];
0
 

Author Comment

by:Mad_Angel
ID: 13513794
that did it, thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question