Error with 'System.NullReferenceException' [modified]
-
I had written :
for(int i=0; i <10; i++){ em[i]->pct = (gcnew System::Windows::Forms::PictureBox()); em[i]->pct->Location = System::Drawing::Point(250, 250); em[i]->pct->Visible = true; em[i]->pct->Size = System::Drawing::Size(50, 50); }
And I got this error :An unhandled exception of type 'System.NullReferenceException' occurred in Little tanks.exe
Additional information: Object reference not set to an instance of an object.
at this line :
em[i]->pct = (gcnew System::Windows::Forms::PictureBox());
what did I do wrong?em[i] is null, you probably created an array and didn't create the objects in it
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
em[i] is null, you probably created an array and didn't create the objects in it
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
the em array defined so :
static array^ em;
and tank is structure :ref struct tank { int step_x, step_y; System::Windows::Forms::PictureBox^ pct; };
What do you mean saying to create objects for array?:)thesad wrote:
static array^ em;
You have not initialized array here. Array can be initialized as given in
MSDN
example.array<MyClass ^> ^ MyArray = gcnew array<MyClass ^>(100);
Prasad Notifier using ATL | Operator new[],delete[][^]
-
thesad wrote:
static array^ em;
You have not initialized array here. Array can be initialized as given in
MSDN
example.array<MyClass ^> ^ MyArray = gcnew array<MyClass ^>(100);
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Would interesting to see, how you have done that. There are two possiblitites, 1. You have not allocated memory for it. 2. You are accessing invalid index.
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Would interesting to see, how you have done that. There are two possiblitites, 1. You have not allocated memory for it. 2. You are accessing invalid index.
Prasad Notifier using ATL | Operator new[],delete[][^]
Here is all code :
#pragma once namespace Littletanks { ref struct tank { int step_x, step_y; System::Windows::Forms::PictureBox^ pct; }; using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class wnd_main : public System::Windows::Forms::Form { public: wnd_main(void) { InitializeComponent(); } protected: ~wnd_main() { if (components) delete components; } private: System::ComponentModel::IContainer^ components; private: int max_x, max_y, min_x, min_y; private: tank usr, b_usr; private: static array^ em; private: static array^ b_em; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->usr.pct = (gcnew System::Windows::Forms::PictureBox()); this->b_usr.pct = (gcnew System::Windows::Forms::PictureBox()); this->SuspendLayout(); // // pct_usr // this->usr.pct->Location = System::Drawing::Point(235, 391); this->usr.pct->Size = System::Drawing::Size(50, 50); this->usr.pct->TabStop = false; // // pct_em // array^ em = gcnew array(10); array^ b_em = gcnew array(10); for(int i=0; i <10; i++){ this->em[i]->pct = gcnew System::Windows::Forms::PictureBox(); this->em[i]->pct->Location = System::Drawing::Point(250, 250); this->em[i]->pct->Visible = true; this->em[i]->pct->Size = System::Drawing::Size(50, 50); this->b_em[i]->pct = gcnew System::Windows::Forms::PictureBox(); this->b_em[i]->pct->Load("b_em[i]->jpg"); this->b_em[i]->pct->Location = System::Drawing::Point(13, 64); this->b_em[i]->pct->Size = System::Drawing::Size(5, 5); this->b_em[i]->pct->Visible = false; this->Controls->Add(this->b_em[i]->pct); this->Controls->Add(this->em[i]->pct); } this->b_usr.pct->Load("b_usr.jpg"); this->b_usr.pct->Location = System::Drawing::Point(97, 142); this->b_usr.pct->Size = System::Drawing::Size(5, 5); this->b_usr.pct->Visible = false; // wnd_main // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::SystemColors::Window; this->ClientSize = System::Drawin
-
Here is all code :
#pragma once namespace Littletanks { ref struct tank { int step_x, step_y; System::Windows::Forms::PictureBox^ pct; }; using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class wnd_main : public System::Windows::Forms::Form { public: wnd_main(void) { InitializeComponent(); } protected: ~wnd_main() { if (components) delete components; } private: System::ComponentModel::IContainer^ components; private: int max_x, max_y, min_x, min_y; private: tank usr, b_usr; private: static array^ em; private: static array^ b_em; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->usr.pct = (gcnew System::Windows::Forms::PictureBox()); this->b_usr.pct = (gcnew System::Windows::Forms::PictureBox()); this->SuspendLayout(); // // pct_usr // this->usr.pct->Location = System::Drawing::Point(235, 391); this->usr.pct->Size = System::Drawing::Size(50, 50); this->usr.pct->TabStop = false; // // pct_em // array^ em = gcnew array(10); array^ b_em = gcnew array(10); for(int i=0; i <10; i++){ this->em[i]->pct = gcnew System::Windows::Forms::PictureBox(); this->em[i]->pct->Location = System::Drawing::Point(250, 250); this->em[i]->pct->Visible = true; this->em[i]->pct->Size = System::Drawing::Size(50, 50); this->b_em[i]->pct = gcnew System::Windows::Forms::PictureBox(); this->b_em[i]->pct->Load("b_em[i]->jpg"); this->b_em[i]->pct->Location = System::Drawing::Point(13, 64); this->b_em[i]->pct->Size = System::Drawing::Size(5, 5); this->b_em[i]->pct->Visible = false; this->Controls->Add(this->b_em[i]->pct); this->Controls->Add(this->em[i]->pct); } this->b_usr.pct->Load("b_usr.jpg"); this->b_usr.pct->Location = System::Drawing::Point(97, 142); this->b_usr.pct->Size = System::Drawing::Size(5, 5); this->b_usr.pct->Visible = false; // wnd_main // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::SystemColors::Window; this->ClientSize = System::Drawin
Here, you had made couple of mistakes,
thesad wrote:
array^ em = gcnew array(10); array^ b_em = gcnew array(10);
Here, again you are defining local variables. It should be like this,
this->em = gcnew array<tank^>(10);
this->b_em = gcnew array<tank^>(10);Again, simply defining array doesn't initializes its members, you need to allocate
tank
variable for each index. So, your code can be modified like this,thesad wrote:
for(int i=0; i <10; i++){ this->em[i]->pct = gcnew System::Windows::Forms::PictureBox(); this->em[i]->pct->Location = System::Drawing::Point(250, 250);
Modify this to,
for(int i=0; i <10; i++){
em[i] = gcnew tank;//create tank object for each index
this->em[i]->pct = gcnew System::Windows::Forms::PictureBox();
this->em[i]->pct->Location = System::Drawing::Point(250, 250);
//your code followsPrasad Notifier using ATL | Operator new[],delete[][^]
-
Here, you had made couple of mistakes,
thesad wrote:
array^ em = gcnew array(10); array^ b_em = gcnew array(10);
Here, again you are defining local variables. It should be like this,
this->em = gcnew array<tank^>(10);
this->b_em = gcnew array<tank^>(10);Again, simply defining array doesn't initializes its members, you need to allocate
tank
variable for each index. So, your code can be modified like this,thesad wrote:
for(int i=0; i <10; i++){ this->em[i]->pct = gcnew System::Windows::Forms::PictureBox(); this->em[i]->pct->Location = System::Drawing::Point(250, 250);
Modify this to,
for(int i=0; i <10; i++){
em[i] = gcnew tank;//create tank object for each index
this->em[i]->pct = gcnew System::Windows::Forms::PictureBox();
this->em[i]->pct->Location = System::Drawing::Point(250, 250);
//your code followsPrasad Notifier using ATL | Operator new[],delete[][^]
-
the em array defined so :
static array^ em;
and tank is structure :ref struct tank { int step_x, step_y; System::Windows::Forms::PictureBox^ pct; };
What do you mean saying to create objects for array?:)= new gcnew tank(); to create the actual objects in the array. where x is the index into the array ( you'd probably do this in a loop )Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
= new gcnew tank(); to create the actual objects in the array. where x is the index into the array ( you'd probably do this in a loop )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
= new gcnew tank();Typo. :)
Prasad Notifier using ATL | Operator new[],delete[][^]