Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Listbox items into strings

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
Mad_Angel
Asked:
Mad_Angel
  • 6
  • 5
1 Solution
 
testnCommented:
possible that your item may be nulll

 items[i] = System::Convert::ToString(this->listBox1->Items->Item[i]);
0
 
Mad_AngelAuthor Commented:
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
 
testnCommented:
you forgot to initialize items
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
testnCommented:
String *items[] = new String[len];
0
 
Mad_AngelAuthor Commented:
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
 
Mad_AngelAuthor Commented:
How do i make that globally?
0
 
Mad_AngelAuthor Commented:
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
 
testnCommented:
it should
///// declare this globally
 String* items[];


/////// put this in your method
items[] = new String*[len];
0
 
Mad_AngelAuthor Commented:
Ok, now this: items[] = new String*[len]; gives me this error message:
error C2059: syntax error : ']'

Thanks again.
0
 
testnCommented:
sorry remove [] i forgot....

items = new String*[len];
0
 
Mad_AngelAuthor Commented:
that did it, thanks
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now