Jam*_*mes 1 java error-handling nullpointerexception
我试图使用一个字符串并将其转换为int来比较第一个第一列和所有行中输入的字符串中的所有数字.当我输入一个数字时,我得到一个NullPointerException.问题是,当我觉得我已经正确地声明了所有对象时,我不明白为什么编译器会告诉我这个.请帮忙!
import java.util.ArrayList;
public class Decoder
{
private int[][] zipdecoder;
private ArrayList<Integer> zipcode;
private String finalCode;
private String bars;
private int place;
public Decoder()
{
int[][] zipdecoder = new int[][]{
{1,0,0,0,1,1},
{2,0,0,1,0,1},
{3,0,0,1,1,1},
{4,0,1,0,0,0},
{5,0,1,0,1,1},
{6,0,1,1,0,0},
{7,1,0,0,0,0},
{8,1,0,0,1,1},
{9,1,0,1,0,0},
{0,1,1,0,0,0}
};
zipcode = new ArrayList<Integer>();
}
public void insertToArray(String zip)
{
int count = 0;
for(int i = 1; i<zip.length()+1;i++)
{
String piece = zip.substring(count, i);
int number = Integer.parseInt(piece);
for(int j = 0;j<10;j++)
{
if(number == zipdecoder[j][0]){
for(int a = 1;a<5;a++)
{
zipcode.add(place,zipdecoder[j][a]);
place++;
}
}
count++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*s_D 10
您没有zipdecoder在构造函数中初始化类成员,而是初始化一个新的局部变量(具有相同的名称).
改变这个
int[][] zipdecoder = new int[][]{
Run Code Online (Sandbox Code Playgroud)
至
zipdecoder = new int[][]{
Run Code Online (Sandbox Code Playgroud)
它应该工作.
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |