Re: noob question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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



On May 9, 10:29 pm, "azz131" <binarys...@xxxxxxxxxxxx> 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?

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();

}
}
}


.



Relevant Pages

  • Re: I need to know if a java class import a package
    ... to test if they can iterate an order an array by themselves). ... public static void main(Stringargs) throws Exception { ...
    (comp.lang.java.programmer)
  • Another noob question
    ... i want to access an array of objects inside a method like this ... class MyClass ... public int x=0; ... public static void show{// Error here ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sending email
    ... I'm even newer to programming email ... using whatever arguments it needs -- uses sendEmail ... line throws an exception, your mailmenssage will never be disposed of. ... public static void forgotPassword ...
    (microsoft.public.dotnet.languages.csharp)
  • How to get the value of an attribute in an XML node?
    ... of an xml file that I am trying to read and get the values of the ... XMLEventReader r = factory.createXMLEventReader; ... catch (Exception ex) ... public static void getGeneRecord{ ...
    (comp.lang.java.databases)
  • Re: [array / List]Unknown number of objects
    ... build the List and then when finished transform the List into an array ... public static void main{ ... of the raw type java.util.List ... unknown number of elements and therefore use an ArrayList, ...
    (comp.lang.java.help)