简体   繁体   中英

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.

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.

The first thing you always should do is this: got to your break points view in your IDE, check "stop on Exception thrown" and perhaps give the name NullPointerException. Then run your code in the debugger and it will stop exactly at the point where the NullPointerException is thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM