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. 'System.NullReferenceException'

'System.NullReferenceException'

Scheduled Pinned Locked Moved Managed C++/CLI
questiongraphicsdata-structures
5 Posts 2 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.
  • K Offline
    K Offline
    Kranti1251984
    wrote on last edited by
    #1

    i've created one DLL (testlib) and i want to use it in my application as an array of its instances ... i'm getting an exception as 'System.NullReferenceException' plz consider the following code .. //------------------------------------------------------------------------- testlib::testlibControl *tmp1 __gc[]; for(int i = 0; i < strength; i++) { tmp1[i] = new testlib::testlibControl; tmp1[i]->Location = System::Drawing::Point(x,y); //exception tmp1[i]->Size = System::Drawing::Size(24, 29); x += 30; this->Controls->Add(tmp1[i]); } //------------------------------------------------------------------------- Why does this code cause exception? Thanks, Kranti

    2 1 Reply Last reply
    0
    • K Kranti1251984

      i've created one DLL (testlib) and i want to use it in my application as an array of its instances ... i'm getting an exception as 'System.NullReferenceException' plz consider the following code .. //------------------------------------------------------------------------- testlib::testlibControl *tmp1 __gc[]; for(int i = 0; i < strength; i++) { tmp1[i] = new testlib::testlibControl; tmp1[i]->Location = System::Drawing::Point(x,y); //exception tmp1[i]->Size = System::Drawing::Size(24, 29); x += 30; this->Controls->Add(tmp1[i]); } //------------------------------------------------------------------------- Why does this code cause exception? Thanks, Kranti

      2 Offline
      2 Offline
      2bee
      wrote on last edited by
      #2

      Hi, I might be wrong but most likely you forgot to initialize your array. testlib::testlibControl *tmp1 __gc[]; // add the following tmp1 = __gc new testlib::testlibControl*[strength]; for(int i = 0; i < strength; i++) { ... However with the new Syntax it should be something like this: array^ tmp1 = gcnew array(strength); Furthermore i do strongly recommend to use the new syntax, because it is far more easy to read. best regards Tobias

      K 1 Reply Last reply
      0
      • 2 2bee

        Hi, I might be wrong but most likely you forgot to initialize your array. testlib::testlibControl *tmp1 __gc[]; // add the following tmp1 = __gc new testlib::testlibControl*[strength]; for(int i = 0; i < strength; i++) { ... However with the new Syntax it should be something like this: array^ tmp1 = gcnew array(strength); Furthermore i do strongly recommend to use the new syntax, because it is far more easy to read. best regards Tobias

        K Offline
        K Offline
        Kranti1251984
        wrote on last edited by
        #3

        ok .. i've made required changes as follows but still i'm getting the same exception testlib::testlibControl *tmp1 __gc []; tmp1 = new testlib::testlibControl* __gc [10]; for(int i = 0; i < strength; i++) { tmp1[i]->Location = System::Drawing::Point(x,y); //exception .... ... } Thanks, Kranti

        2 1 Reply Last reply
        0
        • K Kranti1251984

          ok .. i've made required changes as follows but still i'm getting the same exception testlib::testlibControl *tmp1 __gc []; tmp1 = new testlib::testlibControl* __gc [10]; for(int i = 0; i < strength; i++) { tmp1[i]->Location = System::Drawing::Point(x,y); //exception .... ... } Thanks, Kranti

          2 Offline
          2 Offline
          2bee
          wrote on last edited by
          #4

          Hmm, is it correct that your testlibcontrol class is unmanaged? I mean, as far as i know it should be like this if it's a managed one: testlib::testlibControl __gc* tmp1 __gc []; Though i suppose you have created a managed array of unmanaged testlibcontrol pointers? However you said that it is crashing on this line: tmp1[i]->Location = System::Drawing::Point(x,y); //exception Therefore it can be only one of the two things, either x or y have not been initialized (which is very unlikely) or your array is not properly initialized. IMHO it is related to the second one. Therefore I would suggest that you'll have a close look into your testlibcontrol class (constructor etc.) and the initialization of that array again. Furthermore you should step through your code with the debugger and check if it has really initialized an object of the type testlibcontrol here: tmp1[i] = new testlib::testlibControl; regards Tobias

          K 1 Reply Last reply
          0
          • 2 2bee

            Hmm, is it correct that your testlibcontrol class is unmanaged? I mean, as far as i know it should be like this if it's a managed one: testlib::testlibControl __gc* tmp1 __gc []; Though i suppose you have created a managed array of unmanaged testlibcontrol pointers? However you said that it is crashing on this line: tmp1[i]->Location = System::Drawing::Point(x,y); //exception Therefore it can be only one of the two things, either x or y have not been initialized (which is very unlikely) or your array is not properly initialized. IMHO it is related to the second one. Therefore I would suggest that you'll have a close look into your testlibcontrol class (constructor etc.) and the initialization of that array again. Furthermore you should step through your code with the debugger and check if it has really initialized an object of the type testlibcontrol here: tmp1[i] = new testlib::testlibControl; regards Tobias

            K Offline
            K Offline
            Kranti1251984
            wrote on last edited by
            #5

            hey, thanks a lot .. this type of initialization worked for me ... testlib::testlibControl *tmp1[] = __gc new testlib::testlibControl*[10]; tmp1[i] = new testlib::testlibControl; tmp1[i]->Location = System::Drawing::Point(x,y); tmp1[i]->Size = System::Drawing::Size(24, 29); Thanks, Kranti

            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