我正在从一个文件中读取行,我相信一旦我读取了所有行,就会因为while循环条件而得到一个异常。
Exception in thread "main" java.lang.NullPointerException
at liarliar.main(liarliar.java:79)
…代码…
// read the first line of the file
iNumMembers = Integer.parseInt(br.readLine().trim());
// read file
while ((sLine = br.readLine().trim()) != null) {
// extract the name and value 'm'
String[] split = sLine.split("\\s+");
sAccuser = split[0];
iM = Integer.parseInt(split[1]);
saTheAccused = new String[iM];
for (int i = 0; i < iM; i++) {
saTheAccused[i] = br.readLine().trim();
}
// create member
// initialize name and number of members being accused
Member member = new Member();
member.setName(sAccuser);
member.setM(iM);
member.setAccused(saTheAccused);
veteranMembers.add(member);
}
内部的for循环将必须读取多行,因此如果读取文件的最后一行,则while将尝试
readLine()
这将失败,因为整个文件已经被读取。那么,怎么做呢?
BufferedReader readLine()
工作以及如何安全地退出while循环?
谢谢。