'System.NullReferenceException'

  • Thread starter Thread starter nishi.hirve
  • Start date Start date
N

nishi.hirve

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 = new testlib::testlibControl;
tmp1->Location = System::Drawing::Point(x,y); //exception

tmp1->Size = System::Drawing::Size(24, 29);
x += 30;

this->Controls->Add(tmp1);

}
//-------------------------------------------------------------------------------------------------------


Why does this code cause an exception?

Thanks,
Nishi
 
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 = new testlib::testlibControl;
tmp1->Location = System::Drawing::Point(x,y); //exception

tmp1->Size = System::Drawing::Size(24, 29);
x += 30;

this->Controls->Add(tmp1);

}
Why does this code cause an exception?


Because tmp1 is not initialized.
you declaration of tmp1 declares it to be an array, but you don't actually
allocate an array.
then you start using it as an array pointer that is not assigned to
anything.

allocate an array of 'strength' size before you actually use tmp1 to solve
the problem.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
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->Location = System::Drawing::Point(x,y); //exception

.....
....
}


Thanks,
Nishi
 
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->Location = System::Drawing::Point(x,y); //exception

....
...
}


Yes, but now tmp1 is uninitialized. as a result, tmp1->Location will
result in an illegal assignment,
since tmp1 does not point to anything.
Do

tmp1 = new testlib::testlibControl(...);

before you try to do
tmp1->Location = System::Drawing::Point(x,y);

You have to allocate both the array itself, and the objects that you want to
be in that array.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top