简体   繁体   English

非常具体的Java构造函数和Object类问题

[英]very specific java constructors and Object class problem

I have an array at actionTable.get(state). 我在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. 当我将一个onject添加到数组(即Reduce)时,reduce的属性似乎与它无关。 The array is of type Action[] where Action is the superclass of Reduce, could this be the reason? 该数组的类型为Action [],其中Action是Reduce的超类,这可能是原因吗?

Adding the reduce to the array: 将reduce添加到数组:

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: 检查新添加的reduce是否具有正确的head字段:

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

A NullPointerException occurs on the last print statement. NullPointerException发生在最后一个打印语句上。 The .prod part is defined but the .prod.head is null, even though the original prod object had a defined head. 即使原始prod对象具有已定义的head,也已定义了.prod部分,但.prod.head为null。 This is the constructor for Reduce: 这是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. 定义了构造函数中所有分配的RHS。 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. 因此,我不理解为什么当您通过actionTable访问新的Reduce可以访问的prod对象中的head字段未定义时。

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. 还有一些System.out.println可以帮助您。

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. 您始终应该做的第一件事是:在IDE中进入断点视图,选中“在抛出异常时停止”,并可能命名为NullPointerException。 Then run your code in the debugger and it will stop exactly at the point where the NullPointerException is thrown. 然后在调试器中运行您的代码,它将完全在引发NullPointerException的位置停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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