I have a simple class where the constructor creates an array of objects, and obtain the hash code of each object in the array, as following. At run time, the program throws a null pointer exception when System.identityHashCode(Object x) is called. I have already instantiated the Object. Why might the exception have occurred?
public class ArrayOfObjects
{
private Object[] array;
private int[] arrayIdentity;
public ArrayOfObjects(int N)
{
array = new Object[N];
for (int i = 0; i < N; i ++)
{
array[i] = new Object();
**// The line below throws null pointer exception.**
arrayIdentity[i] = System.identityHashCode(array[i]);
}
}
public static void main(String[] args)
{
ArrayOfObjects arrayOfObjects = new ArrayOfObjects(10);
// Do something.
}
}