Solved Unhandled exception when executing command

Discussion in 'Plugin Development' started by Xp10d3, Feb 22, 2020.

  1. Offline

    Xp10d3

    Hello. I recently updated my plugin to 1.15.2 from 1.14.4 and have encountered a MySQL issue. I get a null error I have never had before. I don't know how to fix it. Please help me fix it.

    Error:
    Code:
    [17:51:03] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'eadd' in plugin Master v1.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[server.jar:git-Bukkit-8160e29]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:148) ~[server.jar:git-Bukkit-8160e29]
            at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:690) ~[server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1606) ~[server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1460) ~[server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(SourceFile:36) ~[server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(SourceFile:9) ~[server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:876) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:869) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeAll(SourceFile:103) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:852) [server.jar:git-Bukkit-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:793) [server.jar:git-Bukkit-8160e29]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at corelia.koc.main.Commands.onCommand(Commands.java:52) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[server.jar:git-Bukkit-8160e29]
            ... 17 more
    
    Commands.java:
    https://hatebin.com/qqlkvccidd

    MySQL class (if needed):
     
  2. Offline

    caderapee

    @Xp10d3
    Is the line ? Are u sure the connection has been set ?
     
  3. Offline

    Xp10d3

    Yes. Do u want me to post my Core class?
     
  4. Offline

    KarimAKL

    @Xp10d3
    1. Yes, that would help greatly since line 52 references the Core class a few times.
    2. You should really use a StringBuilder instead of String + String; here's an example:
    Code:Java
    1. String string = "Hello " + "world"; // This is fine, as it only adds the strings 1 time
    2.  
    3. string = "Some" + " other " + "example"; // This should be written like the following
    4.  
    5. string = new StringBuilder()
    6. .append("Some")
    7. .append(" other ")
    8. .append("example")
    9. .toString();

    Look up "Java StringBuilder vs String Concatenation" in your preferred search engine.
     
  5. Offline

    Xp10d3

    Core.java:
    Code:
    package corelia.koc.main;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin implements Listener {
       
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       
        // MySQL/Database stuff.
        private Connection connection;
        public String host, database, username, password, table;
        public int port;
       
        // Gets the config value.
        FileConfiguration config = getConfig();
    
        // When the server starts up, create the custom config, load the config, setup the MySQL stuff,
        // and register the MySQLSetterGetter class.
        public void onEnable() {
            loadConfig();
            mysqlSetup();
           
            new Commands(this);
           
            this.getServer().getPluginManager().registerEvents(new MysqlSetterGetter(), this);
            saveConfig();
        }
       
        public void onDisable() {
            saveConfig();
        }
       
        // Loads the config (default one).
        public void loadConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        // Connects to the config to get the host, port, database name, username, password, and table.
        // Also sets up the MySQL connection.
        public void mysqlSetup() {
            host = this.getConfig().getString("host");
            port = this.getConfig().getInt("port");
            database = this.getConfig().getString("database");
            username = this.getConfig().getString("username");
            password = this.getConfig().getString("password");
            table = this.getConfig().getString("table");
    
            try {
    
                synchronized (this) {
                    if (getConnection() != null && !getConnection().isClosed()) {
                        return;
                    }
    
                    Class.forName("com.mysql.jdbc.Driver");
                    setConnection(
                            DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
                                    this.username, this.password));
    
                    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        // Gets the connection.
        public Connection getConnection() {
            return connection;
        }
    
        // Sets the connection.
        public void setConnection(Connection connection) {
            this.connection = connection;
        }
       
        public void logFile(String message) {
            try {
                File dataFolder = getDataFolder();
                if (!dataFolder.exists()) {
                    dataFolder.mkdir();
                }
               
                File saveTo = new File(getDataFolder(), "add_log.txt");
                if (!saveTo.exists()) {
                    saveTo.createNewFile();
                }
                FileWriter fw = new FileWriter(saveTo, true);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(message);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    Okay, thanks for the tip :)
     
  6. Offline

    KarimAKL

    @Xp10d3 Try printing the value of 'connection' between the setConnection(...) call, and the "MYSQL CONNECTED" message.

    Also, try printing the value of 'table' after getting the value from the config.
     
    Xp10d3 likes this.
  7. Offline

    Xp10d3

    Okay. Did it :)
    https://hatebin.com/bhllolbssu

    In case I did it wrong here is my MySQL class:
    Code:
    package corelia.koc.main;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.UUID;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import net.md_5.bungee.api.ChatColor;
    public class MysqlSetterGetter implements Listener {
        Core plugin = Core.getPlugin(Core.class);
        // On the player join, run the "createPlayer" method.
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            getCoinOrCreatePlayer(player.getUniqueId(), player);
        }
        // How the player gets gold
        /*
        * @EventHandler public void onPlayerDeath(PlayerDeathEvent event) { Player
        * player = event.getEntity(); Player get = player.getPlayer();
        * updateCoins(get.getPlayer().getUniqueId());
        * getCoins(get.getPlayer().getUniqueId()); }
        */
        // If player exists then send console message saying it was found.
        // If not found, send a console message saying it's not found.
        public boolean playerExists(UUID uuid) {
            try {
                PreparedStatement statement = plugin.getConnection()
                        .prepareStatement("SELECT * FROM " + plugin.table + " WHERE UUID=?");
                statement.setString(1, uuid.toString());
                ResultSet results = statement.executeQuery();
                if (results.next()) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Player Found");
                    return true;
                }
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Player NOT Found");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
        // If player is not found, add it to the database with the UUID, name, and gold
        // amount.
        public int getCoinOrCreatePlayer(final UUID uuid, Player player) {
            int playerCoin = 0;
            try {
                PreparedStatement statement = plugin.getConnection()
                    .prepareStatement("SELECT * FROM " + plugin.table + " WHERE UUID=?");
                statement.setString(1, uuid.toString());
                ResultSet results = statement.executeQuery();
                if (!results.next()) {
                        PreparedStatement insert = plugin.getConnection()
                                .prepareStatement("INSERT INTO " + plugin.table + " (UUID,NAME,GOLD) VALUES (?,?,?)");
                        insert.setString(1, uuid.toString());
                        insert.setString(2, player.getName());
                        insert.setInt(3, 0);
                        insert.executeUpdate();
                        Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Player Inserted");
                 
                } else {
                    playerCoin = results.getInt("GOLD");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return playerCoin;
        }
        public boolean isInt(String s) {
            try {
                Integer.parseInt(s);
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }
        // Update coin value (add 1).
        public void updateCoins(UUID uuid) {
            try {
                PreparedStatement statement1 = plugin.getConnection()
                        .prepareStatement("UPDATE " + plugin.table + " SET GOLD=? WHERE UUID=?");
                ResultSet results = statement1.executeQuery();
                results.next();
                statement1.setInt(1, results.getInt("GOLD") + 1);
                statement1.setString(2, uuid.toString());
                statement1.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        public void getCoins(UUID uuid) {
            try {
                PreparedStatement statement = plugin.getConnection()
                        .prepareStatement("SELECT * FROM " + plugin.table + " WHERE UUID=?");
                statement.setString(1, uuid.toString());
                ResultSet results = statement.executeQuery();
                results.next();
                System.out.print("Player has " + results.getInt("GOLD") + " gold.");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
     
  8. Offline

    KarimAKL

    @Xp10d3 I don't have a lot of time right now (when i'm writing this reply), is there any chance you could point me to the messages? :p

    Also, that's quite a lot of stacktraces, you should probably fix some of those.
     
  9. Offline

    Xp10d3

    @KarimAKL The messages in the stack trace? Here:
    Code:
    [19:04:43] [Server thread/WARN]: Enabled plugin with unregistered PluginClassLoader Master v1.1
    [19:04:43] [Server thread/INFO]: player_data
    [19:04:43] [Server thread/WARN]: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:864)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1707)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1217)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2220)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2015)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:768)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    [19:04:43] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [19:04:43] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [19:04:43] [Server thread/WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [19:04:43] [Server thread/WARN]: at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:385)
    [19:04:43] [Server thread/WARN]: at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:323)
    [19:04:43] [Server thread/WARN]: at java.sql.DriverManager.getConnection(Unknown Source)
    [19:04:43] [Server thread/WARN]: at java.sql.DriverManager.getConnection(Unknown Source)
    [19:04:43] [Server thread/WARN]: at corelia.koc.main.Core.mysqlSetup(Core.java:74)
    [19:04:43] [Server thread/WARN]: at corelia.koc.main.Core.onEnable(Core.java:36)
    [19:04:43] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263)
    [19:04:43] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:344)
    [19:04:43] [Server thread/WARN]: at us.Myles.PWP.TransparentListeners.PerWorldPluginLoader.enablePlugin(PerWorldPluginLoader.java:145)
    [19:04:43] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:416)
    [19:04:43] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:448)
    [19:04:43] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:374)
    [19:04:43] [Server thread/WARN]: at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:448)
    [19:04:43] [Server thread/WARN]: at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:254)
    [19:04:43] [Server thread/WARN]: at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:762)
    [19:04:43] [Server thread/WARN]: at java.lang.Thread.run(Unknown Source)
    
     
  10. Online

    timtower Moderator Moderator

    @Xp10d3 You need to fix them, not post them :p
     
  11. Offline

    Xp10d3

    @timtower I know but I meant this:
    :p I know about the other messages. Those are from other plugins. I didn't fix the NPEs from them
     
  12. Offline

    KarimAKL

    @Xp10d3 You should have two messages, what is "player_data" from, and where is the other message?
     
  13. Offline

    Xp10d3

    I might've written my config wrong. That might be why it doesn't say "MySQL connected". Sorry, it's a stupid mistake. I'll mark the thread solved if that's the issue. Sorry to bother you guys. Stupid mistake; couldn't figure out what was going wrong.
     
  14. Offline

    KarimAKL

    @Xp10d3 No problem, try changing it, and then let us know if it works.
     
    Xp10d3 likes this.
  15. Offline

    caderapee

    @Xp10d3
    there's no connection, that why the line throw an error
     
  16. Offline

    Xp10d3

    Yes, that's what I meant :p I'm testing later today and will let you guys know if that's the issue.
     
  17. Offline

    Xp10d3

    Works! Tysm :) Marking thread as solved.
    EDIT: Oops, sorry Moderators. Forgot to press edit :(
     

Share This Page