This is the scenario:
class A{
int a;
}
class B{
A objectA[]=new A[10] ;
}
class C{
B ObjectB;
public static void main(String[] args){
ObjectB.ObjectA[0].a=1;
}
}
I get a nullpointerexception in main operation. However if I declare just one object of class A, I don’t get the error. Why so? How do I rectify it?
calling
new B()initializes an array of objects of typeA, but none of the member objects. You can rectify it first initializingobjectBand then callingobjectA[i] = new A()for each item in the array.