Reputation: 41
Ok, so I was doing a program which has a config file handler, and what it does is that it reads in the lines from a text file, which looks something like this:
ImagePath=SomePath
The method reads line by line matching the key with the corresponding line, and after that it then searches for the '=' character position in the String, and then substrings it such that the value SomePath
is obtained. However, there is an issue.
Basically, the issue I am facing is that the reader is able to read the file, as I put a statement to print out the lines
variable and it does print out the line read. However, the issue is that this lines
variable would then be manipulated, but an NullPointerException error would occur around there (the lines.substring(0,key.length()-1)
part), suggesting that the lines
variable suddenly became null
even though it is shown that something was read into the variable. Thing is, what happens to this variable in between the print statement and the string manipulation? I am unable to fix this issue. Pardon my code which might look messy or whatnot. I was trying multiple variations of my code to try to mitigate the issue, but it seems to not work. What is shown below is my latest variation of the code.
public static String readConfig(String key) throws IOException
{
BufferedReader reader=null;
try
{
reader=new BufferedReader(new FileReader(config));
}
catch(FileNotFoundException e)
{
System.out.println("Failed to read file. Code: 3");
System.exit(0);
}
String lines=reader.readLine();
System.out.println(lines);
String returnValue=null;
while(true)
{
if(key.equals(lines.substring(0,key.length()-1)))
{
int plusPos=0;
for(int i=0;i<lines.length();i++)
{
if(lines.charAt(i)=='=')
{
plusPos=i;
}
}
if(plusPos==0)
{
reader.close();
return null;
}
returnValue=lines.substring(plusPos,lines.length()-1);
reader.close();
return returnValue;
}
lines=reader.readLine();
}
}
Edit 1: Ok, I see that Sbodd suggested that my code is kinda flawed, but well, the above snippet is just an instance to debug and test exactly which part of the loop does the line variable suddenly read null. Here is my original code snippet:
public static String readConfig(String key) throws IOException
{
BufferedReader reader=null;
try
{
reader=new BufferedReader(new FileReader(config));
}
catch(FileNotFoundException e)
{
System.out.println("Failed to read file. Code: 3");
System.exit(0);
}
String line;
String returnValue;
while((line=reader.readLine())!=null)
{
line=line.trim();
if(key.equals(line.substring(0,key.length()-1)))
{
int plusPos=0;
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)=='=')
{
plusPos=i;
}
}
if(plusPos==0)
{
return null;
}
returnValue=line.substring(plusPos,line.length()-1);
return returnValue;
}
}
return null;
}
Upvotes: 0
Views: 122
Reputation: 1093
Your string comparison will always fail.
if(key.equals(lines.substring(0,key.length()-1)))
Will attempt to match "ImagePat" and "ImagePath", which will fail. This will then cause a second iteration of the loop, with lines
now equal to null.
Your comparison should be:
if (key.equals(lines.substring(0, key.length())))
Also, your return value should be
returnValue = lines.substring(plusPos + 1, lines.length());
to properly return the value.
You should also look at the Properties
class, it will make things a lot easier.
Upvotes: 1
Reputation: 11454
Your code will always throw a NPE when it reaches the end of the file.
From the documentation for BufferedReader: [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.
Since your code is:
lines = reader.readLine();
while (true) {
//do stuff with lines
lines=reader.readLine();
}
when you reach end-of-file, reader.readLine
will return null, you'll still pass your while
check (since "true" is always true), and then you'll try and use that null pointer.
You could just replace while(true)
with while (lines != null)
.
A typical pattern to do this instead would be
while ((lines = reader.readLine()) != null) {
//do stuff with lines
}
(Note that here, you don't make the first call to readLine outside the while loop, since your condition will be tested before the first execution of the loop.)
Upvotes: 2