Having problems initializing a managed array

R

raylopez99

Hi all,

I'm getting a runtime error for the below program, though it compiles
fine. The culprit seems to be the managed array--it doesn't exist.
What am I doing wrong?

RL

// OverL2_simple.cpp : main project file.

#include "stdafx.h"
#include "AClass.h"

using namespace System;

int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");

AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
declaration/definition
for (int j=0; j<(A10->a1->Length); j++){
Console::WriteLine("a1 is: {0}", A10->a1[j]);
}

// Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object.
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
// Aclass.h
#pragma once
using namespace System;

ref class AClass
{
public:
AClass(void);
int pubint;
array<int>^ a1; //this array is causing problems
private:
int privint;

};
//////////////////////////////////////////////
// Aclass.cpp
#include "StdAfx.h"
#include "AClass.h"

//
AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}

/*
//compiles but give runtime error:
Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.
*/
 
B

Bruno van Dooren [MVP VC++]

I'm getting a runtime error for the below program, though it compiles
fine. The culprit seems to be the managed array--it doesn't exist.
What am I doing wrong?
AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}

Subtle. in your constructor, you declare a local variable that has the same
name as your class member. I assume thatthis is copy and paste error.
you initialize it properly, but it hides your a1 member variable, which
remains uninitialized.
Then -in your test code- you dereference a1. since that is an unitialized
pointer, you will get an exception.

Change
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
to
a1 = gcnew array<int>(5);
for(int j=0; j<a1->Length; j++)
a1[j] = j;
to solve the problem
--

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

raylopez99

Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.
 
T

Tamas Demjen

raylopez99 said:
Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.

In C++/CLI, you can initialize the array at dynamic construction, which
is a really nice feature:

a1 = gcnew array<int>(5) {1,2,3,4,5};

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top