我有这个java代码,并在运行它时捕获了一个nullPointerException。通过使用eclipse进行调试,我发现msiLine参数为null,但无法找到原因。
String msiAbsolutePath = msiFiles.getAbsolutePath();
String filePath = msiAbsolutePath.substring(0,msiAbsolutePath.lastIndexOf(File.separator));
new File(filePath+"/Emboss").mkdir();
new File(filePath+"/Report").mkdir();
File files = sort(msiFiles,filePath);
BufferedReader msiReader = new BufferedReader(new FileReader(files));
BufferedReader embReader = new BufferedReader(new FileReader(embFiles));
String[] msiTokens = msiFiles.getName().split("\\.(?=[^\\.]+$)");
String[] embTokens = embFiles.getName().split("\\.(?=[^\\.]+$)");
final String msiFileName = msiTokens[0];
final String embFileName = embTokens[0];
Date date = new Date();
DateFormat dateFormatName = new SimpleDateFormat("MMdd");
PrintWriter msiWrite = new PrintWriter(new BufferedWriter(new FileWriter(filePath+"/Emboss/"+msiFileName+".PRN")));
PrintWriter embWrite = new PrintWriter(new BufferedWriter(new FileWriter(filePath+"/Emboss/"+embFileName+".PRN")));
PrintWriter reportWrite = new PrintWriter(new BufferedWriter(new FileWriter(filePath+"/Report/CS"+dateFormatName.format(date)+".RPT")));
cnt=totalLines=0;
//String msiLine = msiReader.readLine();
String msiLine="";
String embLine = embReader.readLine();
here>>> for(msiLine = msiReader.readLine(); msiLine.length() >= 60; msiLine = msiReader.readLine())
{
embLine = embReader.readLine();
msiWrite.print();
:
:
}
statMessage = "Completed " + current + " out of " + lengthOfTask + ".";
}
:
:
}catch(IllegalStateException | IOException | NullPointerException d){
d.printStackTrace();
}
}
}
File sort(File file, String filepath){
File files = new File(filepath + "\\tempMSIfile.msi");
try{
BufferedReader reader = new BufferedReader(new FileReader(file));
Map<String, String> map=new TreeMap<String, String>();
String line=reader.readLine();
for(line = reader.readLine(); line.length() >= 60; line = reader.readLine()){
map.put(getField(line),line);
}
reader.close();
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(files)));
for(String val : map.values()){
writer.print("" +val);
writer.print(NEWLINE);
}
writer.close();
}catch(IllegalStateException | IOException | NullPointerException d){
LOG.fine("Error, " + d.getMessage());
}
return files;
}
String getField(String line) {
return line.substring(10, 26);//extract value you want to sort on
}有人能解释一下为什么msiLine是空的吗?
我认为这涉及到File files = sort(msiFiles,filePath);,但是eclipse报告说files参数确实有正确的文件/东西(?)分配给它。
如果有人想知道,tempMSIfile.msi不是空的。
发布于 2014-02-11 09:36:28
当到达输入结束时,Reader的.readLine()返回null。因此,您的条件:
msiLine.length() < 60`可以触发NullPointerException。将其更改为在测试长度之前添加一个空测试。
编辑:@KubaSpatny指出,将条件更改为:
msiLine != null && msiLine.length() < 60或者,正如您的评论所建议的那样,将msiLine != null作为条件,并将其作为循环中的第一个指令:
if (msiLine.length() < 60)
continue; // or break;, depending on what you want to do您的sort()方法也面临同样的问题。
发布于 2014-02-11 09:39:08
阅读文献资料 for readLine()
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
这意味着:如果msiLine是循环中的null,则文件已被完全读取。在这种情况下,退出循环:
if(msiLine==null){
break;
}或者将检查添加到循环头中。
发布于 2014-02-11 09:38:46
java.io.BufferedReader#readLine()的Javadoc声明它返回
A String containing the contents of the line, not including
any line-termination characters, or null if the end of the
stream has been reached您可能遇到了文件的末尾。当您到达文件末尾时,为msiLine添加一个空检查以检查。
https://stackoverflow.com/questions/21697967
复制相似问题