[英]Java Trying to debug NullPointerException
我有這個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
為null嗎?
我認為這涉及File files = sort(msiFiles,filePath);
但是eclipse報告“ files
參數確實分配了正確的file / thing(?)。
如果有人想知道, tempMSIfile.msi
不為空。
到達輸入結尾時, 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()
方法也遇到同樣的問題。
閱讀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;
}
或將檢查添加到循環頭中。
用於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添加一個空檢查,以檢查何時到達文件末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.