I am trying to do the following
1 - I need to create a class like this
public class base {
int a;
String b;
public base() {
a = 0;
b = "";
}
}
2 - I need to create a class that creates an array of "base" and sets some values
public class arrayBase {
public base[] ab = new base[2];
public arrayBase() {
ab[0].a = 1;
ab[0].b = "test1";
ab[1].a = 2;
ab[1].b = "test2";
}
}
3 - I need to use "arrayBase" in another class
public class test{
public static void main(String[] args) {
arrayBase p = new arrayBase();
System.out.println(p.ab[0].a);
}
}
When I try this it gives an error
Exception in thread "main" java.lang.NullPointerException.
How can I solve that problem?
baseobject for each slot of the array.ab[0] = new base();etc.baseclass would not compile.ArrayBaseinstead ofarrayBase).