From dcd50bad33f076afd14f6d51f0f67082eb416788 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 20 Nov 2016 12:52:05 +0100 Subject: [PATCH] Fixed NPE when database was not initialized --- .../java/de/epiceric/shopchest/ShopChest.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 4578c23..2ad7ad7 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -42,7 +42,6 @@ public class ShopChest extends JavaPlugin { private String latestVersion = ""; private String downloadLink = ""; private ShopUtils shopUtils; - private File debugLogFile; private FileWriter fw; private WorldGuardPlugin worldGuard; @@ -73,7 +72,7 @@ public class ShopChest extends JavaPlugin { config = new Config(this); if (config.enable_debug_log) { - debugLogFile = new File(getDataFolder(), "debug.txt"); + File debugLogFile = new File(getDataFolder(), "debug.txt"); try { if (!debugLogFile.exists()) { @@ -308,18 +307,20 @@ public class ShopChest extends JavaPlugin { public void onDisable() { debug("Disabling ShopChest..."); - int highestId = database.getHighestID(); + if (database != null) { + int highestId = database.getHighestID(); - for (int i = 1; i <= highestId; i++) { - for (Shop shop : shopUtils.getShops()) { - if (shop.getID() == i) { - shopUtils.removeShop(shop, false); - debug("Removed shop (#" + shop.getID() + ")"); + for (int i = 1; i <= highestId; i++) { + for (Shop shop : shopUtils.getShops()) { + if (shop.getID() == i) { + shopUtils.removeShop(shop, false); + debug("Removed shop (#" + shop.getID() + ")"); + } } } - } - database.disconnect(); + database.disconnect(); + } if (fw != null && config.enable_debug_log) { try { @@ -336,7 +337,7 @@ public class ShopChest extends JavaPlugin { * @param message Message to print */ public void debug(String message) { - if (config.enable_debug_log) { + if (config.enable_debug_log && fw != null) { try { Calendar c = Calendar.getInstance(); 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 */ public void debug(Throwable throwable) { - if (config.enable_debug_log) { + if (config.enable_debug_log && fw != null) { PrintWriter pw = new PrintWriter(fw); throwable.printStackTrace(pw); pw.flush();