Java 从文件生成的数组中存在NullPointerException
*下面的代码从文本文件中的字符串构建“2D”Java 从文件生成的数组中存在NullPointerException,java,null,nullpointerexception,multidimensional-array,Java,Null,Nullpointerexception,Multidimensional Array,*下面的代码从文本文件中的字符串构建“2D”数组。目前,它在以下行返回NullPointException错误: temp=此行分割(分隔符); 我的问题是,我对temp返回null的理解是否正确?如果是,为什么以及如何添加检查null?我对Java相当陌生,这是我第一次尝试从文件中创建array的字符串array* ------编辑----- 以上问题已解决。 对于那些感兴趣的人,下面是返回IndexOutOfBoundsException的代码。特别是行: set(i,fileContent
数组。目前,它在以下行返回NullPointException
错误:
temp=此行分割(分隔符);
我的问题是,我对temp
返回null
的理解是否正确?如果是,为什么以及如何添加检查null
?我对Java相当陌生,这是我第一次尝试从文件中创建array
的字符串array
*
------编辑-----
以上问题已解决。
对于那些感兴趣的人,下面是返回IndexOutOfBoundsException的代码。
特别是行:
set(i,fileContents.get(i).replace(hexLibrary[i][0],hexLibrary[i][1])
System.out.println(“在此之后的信噪比”);
字符串[][]十六进制库;//从LibToArray方法调用replaces数组
hexLibrary=LibToArray();
对于(int i=0;i而言,thisLine
更有可能是null
。如果在读取502行之前输入不足,则会发生这种情况。如果thisLine
不是null
,则thisLine。拆分(分隔符)
将不会返回null
。应始终检查null
行:
for (int j=0; j<502; j++) {
thisLine=br.readLine();
if (thisLine != null) {
temp = thisLine.split(delimiter);
for (int i = 0; i < 2; i++) {
hexLibrary[j][i]=temp[i];
}
} else {
// report error: premature end of input file
break; // no point in continuing to loop
}
}
for(int j=0;j您在读取文件时没有检查流的结尾
如果读取器到达流的末尾,方法readLine
将返回null
。根据文本文件中的行数,您在for
循环的第一个中(在退出之前)到达该点(null
)s、 split()返回的字符串数组可能为null。
要检查这一点,只需在第二个for循环周围添加一个if条件,如下所示:
if(temp==null){/*您的循环在这里*/}
split()
可能会返回一个空数组,但它永远不会返回null
。在null
上执行需要对对象执行的操作时,会引发null
异常。在这种情况下,temp
与NPE无关。您的null
检查结构工作正常。现在我已经完成了获取了一个要处理的IndexOutOfBoundsException
。@InDeep-可能来自一个没有分隔符的行(因此从split
返回的数组少于2个成员)。这是一段不同的代码。特别是使用此数组的搜索结构。如果您感兴趣,我已经在OP中添加了它。
public static String[][] LibToArray()
{
String thisLine;
String[] temp;
String delimiter=",";
String [][] hexLibrary = new String[502][2];
try
{
BufferedReader br= new BufferedReader(new FileReader("hexlibrary.txt"));
for (int j=0; j<502; j++) {
thisLine=br.readLine();
temp = thisLine.split(delimiter);
for (int i = 0; i < 2; i++) {
hexLibrary[j][i]=temp[i];
}
}
}
catch (IOException ex) { // E.H. for try
JOptionPane.showMessageDialog(null, "File not found. Check name and directory."); // error message
}
return hexLibrary;
}
for (int j=0; j<502; j++) {
thisLine=br.readLine();
if (thisLine != null) {
temp = thisLine.split(delimiter);
for (int i = 0; i < 2; i++) {
hexLibrary[j][i]=temp[i];
}
} else {
// report error: premature end of input file
break; // no point in continuing to loop
}
}
public static String[][] LibToArray() {
List<String[]> lines = new ArrayList<>();
String delimiter=",";
try (BufferedReader br= new BufferedReader(new FileReader("hexlibrary.txt"))) {
String line = br.readLine();
while (line != null) {
String[] tmp = line.split(delimiter);
// the next line is dangerous--what if there was only one token?
// should add a check that there were at least 2 elements.
lines.add(new String[] {tmp[0], tmp[1]});
line = br.readLine();
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "File not found. Check name and directory.");
}
String[][] hexLibrary = new String[lines.length][];
lines.toArray(hexLibrary);
return hexLibrary;
}