Search code examples
javaclassconstructorsuperclass

very specific java constructors and Object class problem


I have an array at actionTable.get(state). When I go to add an onject to the array, namely the Reduce, the properties of the reduce don't seem to go with it. The array is of type Action[] where Action is the superclass of Reduce, could this be the reason?

Adding the reduce to the array:

actionTable.get(state)[t] = new Reduce(st.items.get(item).prod);

Checking to see if the field head is defined before adding it:

System.out.println(Prod.prods.get(st.items.get(item).prod).head);

Checking to see if the newly added reduce has the correct head field:

System.out.println(actionTable.get(state)[t].prod.head);

A NullPointerException occurs on the last print statement. The .prod part is defined but the .prod.head is null, even though the original prod object had a defined head. This is the constructor for Reduce:

Reduce(int pr) {
    p = pr;
    length = Prod.prods.get(pr).length;
    prod = Prod.prods.get(pr);

}

All of the RHS of the assignments in the constructor are defined. So, I don't understand why the head field, within the prod object that the new Reduce has access to is not defined when you access it through the actionTable.


Solution

  • Trust inheritance and all. Most likely with arrays is, that different array instances are involved (if you enlarge/copy array references). Some more System.out.println's will help there.