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!
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.