Fixed NPE when database was not initialized

This commit is contained in:
Eric 2016-11-20 12:52:05 +01:00
parent 198f83c738
commit dcd50bad33

View File

@ -42,7 +42,6 @@ public class ShopChest extends JavaPlugin {
private String latestVersion = ""; private String latestVersion = "";
private String downloadLink = ""; private String downloadLink = "";
private ShopUtils shopUtils; private ShopUtils shopUtils;
private File debugLogFile;
private FileWriter fw; private FileWriter fw;
private WorldGuardPlugin worldGuard; private WorldGuardPlugin worldGuard;
@ -73,7 +72,7 @@ public class ShopChest extends JavaPlugin {
config = new Config(this); config = new Config(this);
if (config.enable_debug_log) { if (config.enable_debug_log) {
debugLogFile = new File(getDataFolder(), "debug.txt"); File debugLogFile = new File(getDataFolder(), "debug.txt");
try { try {
if (!debugLogFile.exists()) { if (!debugLogFile.exists()) {
@ -308,18 +307,20 @@ public class ShopChest extends JavaPlugin {
public void onDisable() { public void onDisable() {
debug("Disabling ShopChest..."); debug("Disabling ShopChest...");
int highestId = database.getHighestID(); if (database != null) {
int highestId = database.getHighestID();
for (int i = 1; i <= highestId; i++) { for (int i = 1; i <= highestId; i++) {
for (Shop shop : shopUtils.getShops()) { for (Shop shop : shopUtils.getShops()) {
if (shop.getID() == i) { if (shop.getID() == i) {
shopUtils.removeShop(shop, false); shopUtils.removeShop(shop, false);
debug("Removed shop (#" + shop.getID() + ")"); debug("Removed shop (#" + shop.getID() + ")");
}
} }
} }
}
database.disconnect(); database.disconnect();
}
if (fw != null && config.enable_debug_log) { if (fw != null && config.enable_debug_log) {
try { try {
@ -336,7 +337,7 @@ public class ShopChest extends JavaPlugin {
* @param message Message to print * @param message Message to print
*/ */
public void debug(String message) { public void debug(String message) {
if (config.enable_debug_log) { if (config.enable_debug_log && fw != null) {
try { try {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
String timestamp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(c.getTime()); String timestamp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(c.getTime());
@ -354,7 +355,7 @@ public class ShopChest extends JavaPlugin {
* @param throwable {@link Throwable} whose stacktrace will be printed * @param throwable {@link Throwable} whose stacktrace will be printed
*/ */
public void debug(Throwable throwable) { public void debug(Throwable throwable) {
if (config.enable_debug_log) { if (config.enable_debug_log && fw != null) {
PrintWriter pw = new PrintWriter(fw); PrintWriter pw = new PrintWriter(fw);
throwable.printStackTrace(pw); throwable.printStackTrace(pw);
pw.flush(); pw.flush();