My code is something like:
public class Foo {
public int a;
Bar[] bar = new Bar[10];
a = bar[0].baz;
}
public class Bar {
public int b;
public Bar () { //I tried doing away with this constructor, but that didn't
//fix anything
b = 0;
}
public int Baz () {
//do somthing
}
}
And I get an error message similar to:
Exception in thread "Foo" java.lang.NullPointerException
at whichever line in Foo I try to call any function or value of the class Bar. How do I prevent bar[] from being null?
EDIT: After some fiddling, I finally got it fixed, thanks everyone! However, I couldn't call the constructor to fix things up; I had to create another function and call that function from Main (in my case, class Foo is actually class Main, if that really matters). My final result:
public class Foo {
public int a;
Bar[] bar = new Bar[10];
public Foo () { //this constructor wasn't called for some reason... I checked this
//by using System.out.println... no message was print onscreen
for (int a = 0; a < bar.length; a++)
bar[a] = new Bar();
}
public static void initializeFoo () {
for (int a = 0; a < bar.length; a++)
bar[a] = new Bar();
}
public static void Foo () {
initializeFoo();
a = bar[0].baz;
}
}
Anybody want to help me out with that, or am I supposed to create another question? :)