[英]Null pointer Exception in Java when assigning value to int array
My code shows null pointer exception when i assign values to int array in the object: 当我将值分配给对象中的int数组时,我的代码显示了空指针异常:
public class Cage {
int Value;
String Operator;
int[] placeHolders;
}
Cage o = new Cage();
o.Value = Integer.parseInt(strSplit[0]);
o.Operator = strSplit[1];
for(int i=2;i<strSplit.length;i++) {
o.placeHolders[i] = Integer.parseInt(strSplit[i]);
}
您应该为placeHolders
创建一个int
数组,它只是一个声明,而不是现在的定义。
o.placeHolders = new int[strSplit.length];
I guess that you want to have placeHolders
to hold values without null
values in the first to indexes. 我想您想让placeHolders
在索引的第一个中保留没有null
值的值。
o.placeHolders = new int[strSplit.length - 2];
for (int i = 0; i < strSplit.length - 2; i++) {
o.placeHolders[i] = Integer.parseInt(strSplit[i + 2]);
}
您应该在使用它之前实例化int []对象:
int[] placeHolders = new int[100];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.