Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Error with 'System.NullReferenceException' [modified]

Error with 'System.NullReferenceException' [modified]

Scheduled Pinned Locked Moved Managed C++/CLI
graphicshelpquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T thesad

    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?

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #3

    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 )

    T 1 Reply Last reply
    0
    • C Christian Graus

      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 )

      T Offline
      T Offline
      thesad
      wrote on last edited by
      #4

      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?:)

      P C 2 Replies Last reply
      0
      • T thesad

        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?:)

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #5

        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[][^]

        T 1 Reply Last reply
        0
        • P prasad_som

          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[][^]

          T Offline
          T Offline
          thesad
          wrote on last edited by
          #6

          I wrote taht you had sad, but still at same line is that error

          P 1 Reply Last reply
          0
          • T thesad

            I wrote taht you had sad, but still at same line is that error

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #7

            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[][^]

            T 1 Reply Last reply
            0
            • P prasad_som

              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[][^]

              T Offline
              T Offline
              thesad
              wrote on last edited by
              #8

              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

              P 1 Reply Last reply
              0
              • T thesad

                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

                P Offline
                P Offline
                prasad_som
                wrote on last edited by
                #9

                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 follows

                Prasad Notifier using ATL | Operator new[],delete[][^]

                T 1 Reply Last reply
                0
                • P prasad_som

                  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 follows

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  T Offline
                  T Offline
                  thesad
                  wrote on last edited by
                  #10

                  thank you for your help :)

                  1 Reply Last reply
                  0
                  • T thesad

                    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?:)

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #11

                    = 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 )

                    P 1 Reply Last reply
                    0
                    • C Christian Graus

                      = 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 )

                      P Offline
                      P Offline
                      prasad_som
                      wrote on last edited by
                      #12

                      Christian Graus wrote:

                      = new gcnew tank();

                      Typo. :)

                      Prasad Notifier using ATL | Operator new[],delete[][^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups