In my code I'm trying to accept the arguments and put it in the the array of objects called cl. It gives me NullPointerException.
//In my main class
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
cl[] Array1;
Array1 = new cl[n];
cl[0].a1 = Integer.parseInt(args[1]);
cl[0].a2 = Integer.parseInt(args[2]);
}
and
//another class declaration
public class cl {
int a1;
int a2;
}
I'm getting a NullPointerException, I'm able to print the command line arguments. I'm not sure what the problem is.
arr[] Array1;? is your code not give compile time error?cl[0].a1should beArray1[0].a1. Similarly for the other field access.