PDA

View Full Version : Help please :)



Shuttleu
09-22-2010, 05:53 PM
well i just started learning java again and i forgot nearly everything

anyway so far i have this

class First {
int x = 2;
}

public class Main {

public static void main(String[] args)
{
First[] testing = new First[5];

testing[2].x = 5;

System.out.println(testing[2].x);
}
}

it compiles fine but when i run it i get

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:11)


any ideas are welcome

~shut

Yakman
09-28-2010, 04:54 PM
change to this.



class First {
int x = 2;
}

public class Main {

public static void main(String[] args)
{
First[] testing = new First[5];
testing[2] = new First();
testing[2].x = 5;

System.out.println(testing[2].x);
}
}

Shuttleu
09-28-2010, 05:56 PM
change to this.



class First {
int x = 2;
}

public class Main {

public static void main(String[] args)
{
First[] testing = new First[5];
testing[2] = new First();
testing[2].x = 5;

System.out.println(testing[2].x);
}
}


hmmm... ok, thanks :)

so if i was to create a array that had a length of 1000, then i would have to do that for each one?

if i do then i know i could obviously just use a loop

~shut

arash
10-03-2010, 04:39 AM
You should create a constructor for your inner class and using that constructor initialize your array. It is much simpler.