我有这个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
不是空的。
答案 0 :(得分:3)
Reader
的{{1}}会返回.readLine()
。因此,你的条件是:
null
可以触发msiLine.length() < 60`
。更改它以在测试长度之前添加空测试。
编辑:正如@KubaSpatny指出的那样,将条件更改为:
NullPointerException
或者,正如您的评论似乎建议的那样,将msiLine != null && msiLine.length() < 60
作为条件,并作为循环中的第一条指令:
msiLine != null
您的if (msiLine.length() < 60)
continue; // or break;, depending on what you want to do
方法遇到同样的问题。
答案 1 :(得分:2)
阅读readLine()
的{{3}}:
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;
}
或将检查添加到循环标题中。
答案 2 :(得分:0)
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的空检查以检查何时到达文件末尾。