Reputation: 120
I have this java code and caught a nullPointerException when I run it.
Through debugging using eclipse, I found out that msiLine
parameter is null but I can't find out why.
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
}
Can somebody explain to me why is the msiLine
is null?
I think it involves the File files = sort(msiFiles,filePath);
but the eclipse report that files
parameter does have the correct file/thing(?) assigned to it.
In case anyone wondering, tempMSIfile.msi
is not empty.
Upvotes: 0
Views: 597
Reputation: 121712
Reader
's .readLine()
returns null
when the end of input is reached. As such, your condition:
msiLine.length() < 60`
can trigger a NullPointerException
. Change it to add a null test before testing the length.
EDIT: as @KubaSpatny points out, change the condition to:
msiLine != null && msiLine.length() < 60
Or, as your comment seems to suggest, put msiLine != null
as the condition, and as the first instruction in the loop:
if (msiLine.length() < 60)
continue; // or break;, depending on what you want to do
Your sort()
method suffers the same problem.
Upvotes: 3
Reputation: 11934
Read the documentation 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
That means: if msiLine
is null
inside the loop, the file has been read completely. In that case, exit the loop:
if(msiLine==null){
break;
}
or add the check to your loop header.
Upvotes: 2
Reputation: 3129
Javadoc for java.io.BufferedReader#readLine() states that it 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
You are probably encountering the end of the file. Add a null check for msiLine to check when you reach the end of the file.
Upvotes: 0