probs adding objects to array ( simple )

A

andrewcw

Seems it should be simple. I had problems with a larger
class and the newsgroup suggested something I also tried.
So I pruned my class to see if I could find out what could
be wrong, Still dont know. This snippet is easily dropped
into any project, THANKS

namespace QCLoader
{
public class disksetitem
{
public string model;
}
public class mytestrecord
{
public disksetitem[] disksetitem;
}
}

/// test :
try
{
QCLoader.mytestrecord rec = new QCLoader.mytestrecord();
rec.disksetitem[0] = new QCLoader.disksetitem(); //CRASH
rec.disksetitem[0].model="foobar";
}
catch ( Exception e)
{
Console.WriteLine(e.Message);
}
 
J

Jeffrey Tan[MSFT]

Hi andrew,

In your code, the mytestrecord's disksetitem member variable is a pointer
that points to
an array of disksetitem type items.
But till the executive of "rec.disksetitem[0] = new
QCLoader.disksetitem(); ", rec.disksetitem[0] has
not been created. rec.disksetitem only points to null.
So this sentence will generate an exception of {"Object reference not set
to an instance of an object." }
You should initialize the rec.disksetitem first, such as:
rec.disksetitem=new QCLoader.disksetitem[5];
rec.disksetitem[0] = new QCLoader.disksetitem();

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "andrewcw" <[email protected]>
| Sender: "andrewcw" <[email protected]>
| Subject: probs adding objects to array ( simple )
| Date: Mon, 18 Aug 2003 18:23:32 -0700
| Lines: 29
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNl8IEYLeqHihHHRGWFLQuyji/2xQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:177282
| NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Seems it should be simple. I had problems with a larger
| class and the newsgroup suggested something I also tried.
| So I pruned my class to see if I could find out what could
| be wrong, Still dont know. This snippet is easily dropped
| into any project, THANKS
|
| namespace QCLoader
| {
| public class disksetitem
| {
| public string model;
| }
| public class mytestrecord
| {
| public disksetitem[] disksetitem;
| }
| }
|
| /// test :
| try
| {
| QCLoader.mytestrecord rec = new QCLoader.mytestrecord();
| rec.disksetitem[0] = new QCLoader.disksetitem(); //CRASH
| rec.disksetitem[0].model="foobar";
| }
| catch ( Exception e)
| {
| Console.WriteLine(e.Message);
| }
|
 
D

David Lau

andrewcw said:
Seems it should be simple. I had problems with a larger
class and the newsgroup suggested something I also tried.
So I pruned my class to see if I could find out what could
be wrong, Still dont know. This snippet is easily dropped
into any project, THANKS

namespace QCLoader
{
public class disksetitem
{
public string model;
}
public class mytestrecord
{
public disksetitem[] disksetitem;
}
}

/// test :
try
{
QCLoader.mytestrecord rec = new QCLoader.mytestrecord();

//here you should add statement as following
//num is number of elements that the array has.

disksetitem = new QCLoader.disksetitem[num];
rec.disksetitem[0] = new QCLoader.disksetitem(); //CRASH
rec.disksetitem[0].model="foobar";
}
catch ( Exception e)
{
Console.WriteLine(e.Message);
}
 

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