非常具体的java构造函数和对象类问题

发布于 2024-12-06 09:35:30 字数 865 浏览 0 评论 0原文

我在 actionTable.get(state) 有一个数组。 当我向数组添加一个对象(即Reduce)时,Reduce 的属性似乎并没有随之而来。 该数组的类型为 Action[],其中 Action 是 Reduce 的超类,这可能是原因吗?

将reduce添加到数组中:

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

添加之前检查字段头是否已定义:

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

检查新添加的reduce是否具有正确的头字段:

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

最后一个打印语句出现NullPointerException。尽管原始 prod 对象具有已定义的头,但 .prod 部分已定义,但 .prod.head 为 null。 这是Reduce 的构造函数:

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

}

构造函数中所有赋值的RHS 均已定义。所以,我不明白为什么当您通过 actionTable 访问它时,新的 Reduce 可以访问的 prod 对象中的 head 字段没有定义。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-12-13 09:35:30

相信继承和一切。对于数组来说,最有可能的是涉及不同的数组实例(如果放大/复制数组引用)。更多的 System.out.println 会有所帮助。

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.

破晓 2024-12-13 09:35:30

您应该做的第一件事是:进入 IDE 中的断点视图,选中“抛出异常时停止”,并可能指定名称 NullPointerException。
然后在调试器中运行代码,它将恰好在抛出 NullPointerException 的地方停止。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文