[英]Java: Add elements ArrayList
I have this code that tells me a NullPointerException
precisely at the method inizializza()
at line:我有这段代码告诉我
NullPointerException
正好在方法inizializza()
行:
valori[y].add(new Record(matriceBinaria[i][j], j));
How can I add items to the valori
without that kind of exception?如何在没有这种例外的情况下向
valori
添加项目?
You must also initialize each element in you array as follows :您还必须按如下方式初始化数组中的每个元素:
if (valori[y] == null)
valori[y] = new ArrayList();
valori[y].add(new Record(matriceBinaria[i][j], j));
ArrayList[size]
is actually an array where each element is the ArrayList
object. ArrayList[size]
实际上是一个数组,其中每个元素都是ArrayList
对象。 Since ArrayList
is not a primitive you must alloacte memory for it by using new
.由于
ArrayList
不是原语,因此您必须使用new
为其分配内存。 So when you do valori[y]
in your code you are accessing an uninitialized (null) object因此,当您在代码中执行
valori[y]
,您正在访问一个未初始化的(空)对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.