简体   繁体   中英

MySQL Java Bukkit Plugin - Select from database

I have a problem in my Bukkit plugin when I'm trying to get a message from MySQL. I have a table called "ReportsLog" that store a "SenderName" and a "Message".

I think Bukkit knowledge isn't a must to solve this problem, the main error is:

     [01:44:19 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command          'areport' in plugin StaffTrainer v1.0

This is the error:

 [01:44:19 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command          'areport' in plugin StaffTrainer v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
got.jar:git-Spigot-f928e7a-e91aed8]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
1) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
r.java:642) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon
nection.java:1135) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
:970) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
:45) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
:1) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
3) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
?:1.8.0_45]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
ot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
18) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
67) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
57) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
:560) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.NullPointerException
    at code.katzuno.StaffTrainer.onCommand(StaffTrainer.java:80) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
got.jar:git-Spigot-f928e7a-e91aed8]
    ... 15 more

This is the code at line 80 (the code with the error):

            String ReportSender = args[0];
            String mesaj = mess.getMessage(ReportSender);

Most probably the error is in the function getMessage. This is the code of the function getMessage()

   public String getMessage(String PlayerName) {
        String mesaj = " ";
        if (playerExists(PlayerName)) {
            try {
                ResultSet rs = StaffTrainer.mysql.query("SELECT * FROM ReportsLog WHERE SenderName= '" + PlayerName + "'");
                if ( (!rs.next())  || (String.valueOf(rs.getString("Message")) == null) );
                mesaj = rs.getString("Message");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            createPlayer(PlayerName);
            getMessage(PlayerName);
        }
        return mesaj;
    }

The args variable is null. It says that the null pointer exception comes from that line and the only place you're trying to access a value is from that array. You'll want to make a check beforehand like this:

if (args != null) {
    ...
}

to prevent the exception from occuring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM