Reputation:
I have been writing a Java program - Specifically a Bukkit plugin, although that has nothing to do with the question - and for a reason I cannot see I am getting a NullPointerException. Excerpted below is the code at which there is an error. getPath()
is well-tested and yields the correct directory - That is, the one in which the config file should be in. the config file is created, and should have been written to. The tester file I have has been successfully created, and so should the config file.
File config = new File(getPath("config"));
BufferedReader in = new BufferedReader(new FileReader(config));
skipLines(11, in);
if(in.readLine().equalsIgnoreCase("true")) { //Exception is here
System.out.println("Successfully wrote to config file");
}
else {
System.out.println("Failed to write text correctly to config file");
}
in.close();
The whole thing is enclosed in a try/catch statement.
If you need any more info, just ask and I will be happy to provide.
NOTE: The file is working just fine - When I check manually the file has writing on it. That's why the problem exists.
The code that writes to the file is below:
File exConfig = new File(System.getProperty("user.dir") + File.separator + "configExample");
PrintWriter out = new PrintWriter(new FileWriter(config));
BufferedReader in = new BufferedReader(new FileReader(exConfig));
for(int i = 1; i <= configLength; i++) {
out.println(in.readLine());
}
out.flush();
out.close();
System.out.println("Successfully wrote defaults to config");
configLength
is a variable equal to the number of lines in the config. I have tested it and it works.
There may be something odd there. The file called "configExample" is as follows:
########################################
# hPlugin is written by newbiedoodle #
########################################
# ----- Plugin config file ----- #
########################################
hPlugin
v.0.3
NOTE: Do not edit the config file besides changing the values - it may result in errors.
--------------
Enable hPlugin?
true
Strikes before being banned?
3
Godmode?
true
First time running the plugin?
true
Curse protection?
true
Emergency shelter?
true
Path building?
true
Blocked words/phrases (Separate with colon (:)):
shit:fuck:damn:bitch:hell
As you can see, this SHOULD be more than 11 lines... And if the 11th line isn't "true" then it still shouldn't cause an error.
Well, after a little bit of fooling around, I got it to work... Turns out, I had a variable screwed up because a function had a filename hard-coded into it rather than set to the variable I was trying to pass. Thanks for all the help though!
Upvotes: 1
Views: 1618
Reputation: 28767
readLine() returns null, and then you call equalsIgnore on that null value.
separate that calls, and check to be not null.
Upvotes: 0
Reputation: 46438
The exception is obvious, there is nothing to read from your BufferedReader. Thus in.readLine()
returns null, Thus invoking in.readLine.equalIgnorecase()
returns NullPointerException.
you should first check if BufferedReader.readLine() returns null, before you proceed.
if(in.readLine().equalsIgnoreCase("true")) { //Exception is here
should be
String line=null;
while((line=in.readLine())!=null) { //if in.readLine() returns null, the condition fails
if(line.equalsIgnoreCase("true")) {
//do your stuff
}
}
Upvotes: 2