Hello Sam,
Actually I guess
1.
You should first learn the syntax before starting to write programs.
2. If you are not sure then check for the syntax every time.
I admit it is a good question. here is the answer.
int [][]a = new int[2][3]; means we are allocating 2 by 3 integer array and assigning it to 'a'. So this will have 2 rows and 3 columns. left index determines ROW and right index determines COLUMN.
The syntax for double-subscripted arrays, the one you used FOR THIS PROGRAM TO RUN int [][]a = new int[1][]; is meaning that
the double-subsripted array 'a' is having 1 row. But no columns.
So in the next line you got to specify the number of columns the row should have inorder to use it. you can do it by
a[0] = new int[1]; to allocate 1 column for the row a[0]
Then you can assign some value to it as you did.
a[0][0] = 12;
Here is the code:
public class MyClass{
public static void main(
String args[]){
int[][]a = new int[1][];
a[0] = new int[1];
a[0][0] = 12;
System.out.println( a[0][0]);
}
}
This eliminates the runtime NullPointerException.
I guess this helps.
You can aswell refer to Complete Reference
Java 2 or Deitel and Deitel for more understanding.
Thanks.