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