This bug took around 4 hours to fix. It wasn't anything too complicated, but I was looking in the wrong class. I just thought it'd be good to paste the incorrect and correct code to explain why exactly it was confusing me.
I was in my SQLConnection class on the server, which handles the creation of databases and user permissions. I was working with a properties file, named "server.properties", which handled basic server functions, such as if the SQLite Database existed, etc, so that it wouldn't recreate the database every single time it started.
This ran into a problem. The server.properties file wasn't loading correctly, and was therefore resetting every single time the server ran. This messed up a ton of other things, the most important being that it would also reset the SQLite Database every single time also because it thought that the server was running for the first time.
The fix was extremely simple. It was in my main class:
The code was right at the top.
java.util.Properties properties = new java.util.Properties();
if(properties == null){
properties.load(new FileInputStream(new File("server.properties")));
}
The problem was pretty basic. Every time the first line was called, it would make a new null Properties. The second line checked if they were null, which they obviously always were. It then reloaded a new file and stored new properties to that file, resulting in rewriting the properties file every single time the server stopped.
It was fixed with this line:
java.util.Properties properties = new java.util.Properties();
if(!new File("server.properties").exists()){
properties.store(new FileOutputStream("server.properties"), "");
}
properties.load(new FileInputStream("server.properties"));
This line instantiates a new Properties like normal, and then loads from the file. If the file doesn't exist, it will create one. This way, if the file is empty, I can create the property file's properties below.
if(properties.getProperty("propertySetup") == null || properties.getProperty("propertySetup").equalsIgnoreCase("false")){
properties.setProperty("propertySetup", "true");
}
Then, it can rewrite new properties. That way, I never have to worry about a blank properties file.
I just posted this to show how easily a single line of code can mess up the functions of an entire program, and it can often take quite a while to fix if it doesn't display an error!
This is by far the best post you have made! Please make a lot more like it, in the sense that you refer *specifically* to a problem you are trying to solve, present and discuss the code you use to solve it. That's what I'm looking for in these posts.
ReplyDelete