Connecting Tech Pros Worldwide Forums | Help | Site Map

probs adding objects to array ( simple )

andrewcw
Guest
 
Posts: n/a
#1: Nov 15 '05
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);
}

Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: probs adding objects to array ( simple )



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" <andrew.c.watts@boeing.com>
| Sender: "andrewcw" <andrew.c.watts@boeing.com>
| Subject: probs adding objects to array ( simple )
| Date: Mon, 18 Aug 2003 18:23:32 -0700
| Lines: 29
| Message-ID: <089301c365f0$811884d0$a101280a@phx.gbl>
| 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);
| }
|

David Lau
Guest
 
Posts: n/a
#3: Nov 15 '05

re: probs adding objects to array ( simple )



"andrewcw" <andrew.c.watts@boeing.com> wrote in message
news:089301c365f0$811884d0$a101280a@phx.gbl...[color=blue]
> 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();[/color]

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

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


Closed Thread