Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.

noob question

1 వీక్షణ
మొదటి చదవని మెసేజ్‌కు స్కిప్ చేయి

azz131

చదవనివి,
9 మే, 2007 10:29:57 PM09-05-07
వరకు
Hi, i am trying to access a field in an array of objects but i get this
error "Exception System.NullReferenceException was thrown in debuggee:Object
reference not set to an instance of an object".What am i doing wrong?

using System;

using System.Collections.Generic;

namespace ObjectArray

{

class MyClass

{

public int x=0;

}


class MainClass

{

string a;

public static void Main(string[] args)

{

MyClass[] myobject=new MyClass[10];

myobject[1].x=10;// error here


Console.ReadLine();

}

}

}

Dom

చదవనివి,
9 మే, 2007 11:07:49 PM09-05-07
వరకు
You've only created an array of 10 objects. Each one is null. You
need to create and object for each of them.

myobject[0] = new MyClass (...);
...
myobject[9] = new MyClass (...);


Dom

Jon Skeet [C# MVP]

చదవనివి,
10 మే, 2007 2:37:40 AM10-05-07
వరకు
azz131 <binar...@ntlworld.com> wrote:
> Hi, i am trying to access a field in an array of objects but i get this
> error "Exception System.NullReferenceException was thrown in debuggee:Object
> reference not set to an instance of an object".What am i doing wrong?

This line:

MyClass[] myobject=new MyClass[10];

creates an array of 10 elements. Initially, every element is a null
reference. That hasn't created any instances of MyClass itself. So you
need to do:

myobject[0] = new MyClass();

before you can do myobject[0].x = 10;

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

0 కొత్త మెసేజ్‌లు