当我为对象中的 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];
关于java - 为int数组赋值时Java中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12527052/