1.12.2 (MySQL) Getting top 10 stats.

Discussion in 'Spigot Plugin Development' started by CyborgManCZ, Feb 5, 2020.

  1. Okay, so im using MySQL to get the top stats from my database and putting it into a hologram.
    My code:
    Code (Text):
        try {
    ResultSet rs = Database.query("SELECT * FROM Stats ORDER BY " + htype.getSQLColumun() + " DESC LIMIT 10");
          while (rs.next()) {
            topPlayers.put(lastid, rs.getString("user"));
            topCount.put(lastid, rs.getInt(htype.getSQLColumun()));
            lastid++;
          }
       
        } catch (SQLException ex) {
         
          Main.log(ex.getMessage(), HoloCore.class);
        }
    My query() :
    Code (Text):
        public static ResultSet query(String query) {
            ResultSet rs = null;
            try {
                Statement stmt = Main.i.sql.getConn().createStatement();
                stmt.executeQuery(query);
                return stmt.getResultSet();
            }
            catch (Exception ex) {
                Main.log(ex.getMessage(), Database.class);
                return rs;
            }
        }
    and im getting "null" here:
    while (rs.next()) {

    Someone knows how to fix it?
    Thanks :)
     
  2. Your query is returning null then, so either the result set from the statement is null, or an exception happened while trying to execute the query method.
     
  3. Yes, and im trying to figure out why. But i dont know, i have database connected, and everything works
     
  4. Can you please send your code from the query method here?
     
  5. the query() method is already in the first post.
    Code (Text):
        public static ResultSet query(String query) {
            ResultSet rs = null;
            try {
                Statement stmt = Main.i.sql.getConn().createStatement();
                stmt.executeQuery(query);
                return stmt.getResultSet();
            }
            catch (Exception ex) {
                Main.log(ex.getMessage(), Database.class);
                return rs;
            }
        }
     
  6. Oh, sorry. I missed that snippet.
    Can you please send thrown stacktrace from the console? Maybe sql field, or getConn() getter is throwing null?
     
  7. Code (Text):
    [15:03:46] [Server thread/INFO]: PLUGIN >> Loading holograms..
    [15:03:46] [Server thread/INFO]: PLUGIN_DATABASE >> null
    [15:03:46] [Server thread/ERROR]: Error occurred while enabling Plugin v1.0 (Is it up to date?)
    java.lang.NullPointerException: null
        at cz.cyborgman.Plugin.holo.HoloCore.createTopHologram(HoloCore.java:42) ~[?:?]
        at cz.cyborgman.Plugin.Main.onEnable(Main.java:102) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:374) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:421) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:382) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:337) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Main.java:102 is loading the holograms, HoloCore.java:42 is while (rs.next()) {
     
  8. above the stacktrace plugin broadcasts, that plugin_database is null. I can assume that plugin can't connect with the database.

    1. Check if you are registering class instance from sql field in the proper place. Maybe you are trying to connect with the database after registering the class where you are using your database?
    2. Check if you are trying to connect with the database at all.
    3. Are you creating the connection for getConn() method properly?
     
  9. Hi,
    the database is connected, im normally viewing the stats of the player, and everything works, after i added top 10 players in holograms, it just does not work.
     
    #9 CyborgManCZ, Feb 5, 2020
    Last edited: Feb 5, 2020