我有一个NullPointerException,尽管我没有任何空对象。为什么会这样?

huangapple go评论80阅读模式
英文:

I have a NullPointerException even though I don't have any null objects. Why is this?

问题

I am trying to make a neural network for a reinforcement AI in Java 14, and I have the Main class and the AGENT class separate to improve readability. In the main() method, I create a new AGENT object like so on line 25:

AGENT agent = new AGENT();

and when I run the program, I expect to have the object created without an exception and run some function calls, but instead I get a NullPointerException:

Exception in thread "main" java.lang.NullPointerException
	at com.company.AGENT.<init>(AGENT.java:19)
	at com.company.Main.main(Main.java:25)

When I looked in the AGENT class to try and debug, I found line 19:

neural_network[j].add(new Node());

and I was confused, as neither the Node object, the neural_network variable, or the ArrayList contained inside of the neural_network array should have been null. I checked to see if there was a problem with my definition of the neural_network array:

private ArrayList<Node>[] neural_network = new ArrayList[5];

but the definition seemed okay. Finally I checked if adding a <Node> parameter to the definition of the ArrayList would help, or if removing the <Node> parameter from the typecast would help, upon doing which I got errors which terminated my program, instead of the runtime error I was getting from the NullPointerException. I am not sure where the null object is that is causing the issue.

To reproduce, create an object in the Main class inside of the main() method; inside the object's class, create a private variable with an array of ArrayLists which store a third object, and add new objects of the third type to the ArrayLists.

英文:

I am trying to make a neural network for a reinforcement AI in Java 14, and I have the Main class and the AGENT class separate to improve readability. In the main() method, I create a new AGENT object like so on line 25:

AGENT agent = new AGENT();

and when I run the program, I expect to have the object created without an exception and run some function calls, but instead I get a NullPointerException:

Exception in thread &quot;main&quot; java.lang.NullPointerException
at com.company.AGENT.&lt;init&gt;(AGENT.java:19)
at com.company.Main.main(Main.java:25)

When I looked in the AGENT class to try and debug, I found line 19:

neural_network[j].add(new Node());

and I was confused, as neither the Node object, the neural_network variable, or the ArrayList contained inside of the neural_network array should have been null. I checked to see if there was a problem with my definition of the neural_network array:

private ArrayList&lt;Node&gt;[] neural_network = new ArrayList[5];

but the definition seemed okay. Finally I checked if adding a <Node> parameter to the definition of the ArrayList would help, or if removing the <Node> parameter from the typecast would help, upon doing which I got errors which terminated my program, instead of the runtime error I was getting from the NullPointerException. I am not sure where the null object is that is causing the issue.

To reproduce, create an object in the Main class inside of the main() method; inside the object's class, create a private variable with an array of ArrayLists which store a third object, and add new objects of the third type to the ArrayLists.

答案1

得分: 1

你已经初始化了数组,但尚未初始化数组的元素。在你的 AGENT 构造函数中:

for (int i = 0; i &lt; neural_network.length; i += 1) {
    neural_network[i] = new ArrayList&lt;&gt;();
}

在尝试添加 Node 之前。

英文:

You have initialized the array but not the elements of the array. In your AGENT constructor:

for (int i = 0; i &lt; neural_network.length; i += 1) {
    neural_network[i] = new ArrayList&lt;&gt;();
}

before trying to add the Node.

huangapple
  • 本文由 发表于 2020年8月11日 12:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63351393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定