The following code:
public class NullFreeArray {
static primitive class Point {
int x = 0, y = 0;
}
static void setNull(Object[] a) {
try {
a[0] = null;
} catch(Throwable t) {
System.out.println(t);
}
}
public static void main(String[] args) {
Point[] a = new Point[10];
setNull(a);
}
}
produces this output:
java.lang.NullPointerException: Cannot store to object array because "<parameter1>" is null
However, <parameter1> in method setNull() is not null, the NPE is caused by the null reference that the code tries to write to the null-free Point array.
public class NullFreeArray {
static primitive class Point {
int x = 0, y = 0;
}
static void setNull(Object[] a) {
try {
a[0] = null;
} catch(Throwable t) {
System.out.println(t);
}
}
public static void main(String[] args) {
Point[] a = new Point[10];
setNull(a);
}
}
produces this output:
java.lang.NullPointerException: Cannot store to object array because "<parameter1>" is null
However, <parameter1> in method setNull() is not null, the NPE is caused by the null reference that the code tries to write to the null-free Point array.