1

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!

3
  • can you post the stacktrace? Commented Dec 7, 2012 at 3:15
  • You say the file "should" be there. Is it? It has more than 11 lines? Commented Dec 7, 2012 at 3:15
  • @Dchan By stacktrace I mean what I get from e.printStackStrace(), so here it is: java.lang.NullPointerException at minecraft.hplugin.main.mainClass.main(mainClass.java:269) @madth3 The file IS there, I manually checked and it seems like it should work. The file is 25 lines give or take 1 as I counted it rather sloppily. Commented Dec 7, 2012 at 14:45

2 Answers 2

2

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
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

No, the line should be written - I check the file manually and it worked perfectly. That's why I have a question. If it was just that the file was empty, then I would be able to fix it, but it's not.
@NickHartley you are calling readLine on bufferedreader, not file.i dont think there is readLine() method in File class. buffredReader.readLine() returns a string, if the file is empty it returns null. check the doc here docs.oracle.com/javase/7/docs/api/java/io/…
@NickHartley if your file is not empty, can you post the skipLine() method, maybe there is something fishy there
First, you have NO idea how annoying your name it to type :P Second, it turns out I had a file hard-coded into a function I was using, and that's what was causing the problem. Third, I'm pretty sure that I was calling readLine() on a BufferedReader. Thanks anyway though.
@NickHartley hahaha, you dont need to type my name, if you leave a comment i get the notification :P
|
0

readLine() returns null, and then you call equalsIgnore on that null value.
separate that calls, and check to be not null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.