From 23c82cc6ed2358e57399ac772290900c1ff7d437 Mon Sep 17 00:00:00 2001 From: Craftinators Date: Mon, 27 Feb 2023 15:34:42 -0500 Subject: [PATCH] fix: additional null checks --- .../hmccosmetics/database/types/MySQLData.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java index 4911257d..cc41a949 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java @@ -4,6 +4,7 @@ import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.config.DatabaseSettings; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.sql.*; import java.util.Properties; @@ -18,6 +19,7 @@ public class MySQLData extends SQLData { private String password; private int port; + @Nullable private Connection connection; @Override @@ -73,7 +75,7 @@ public class MySQLData extends SQLData { try { if (isConnectionOpen()) { return; - } else close(); // Close connection if still active + } else if (connection != null) close(); // Close connection if still active } catch (RuntimeException e) { e.printStackTrace(); // If isConnectionOpen() throws error } @@ -92,8 +94,9 @@ public class MySQLData extends SQLData { public void close() { Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> { try { + if (connection == null) throw new NullPointerException(); connection.close(); - } catch (SQLException e) { + } catch (SQLException | NullPointerException e) { System.out.println(e.getMessage()); } }); @@ -125,8 +128,9 @@ public class MySQLData extends SQLData { } try { + if (connection == null) throw new NullPointerException(); ps = connection.prepareStatement(query); - } catch (SQLException e) { + } catch (SQLException | NullPointerException e) { e.printStackTrace(); }