My code shows null pointer exception when i assign values to int array in the object:
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.
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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.