Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

Having problems initializing a managed array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
raylopez99 - 28 Dec 2006 00:54 GMT
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.
*/
Bruno van Dooren [MVP VC++] - 28 Dec 2006 07:18 GMT
> 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
Signature


Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

raylopez99 - 28 Dec 2006 08:55 GMT
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.
RL

> 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.
[quoted text clipped - 16 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"
Tamas Demjen - 02 Jan 2007 20:46 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.