Solved NullPointerException with MySQL Query (Kick Function)

Discussion in 'Plugin Development' started by Dealyise, Jul 10, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Dealyise

    The command works fine, but I get a NPE after he did the command.
    He crashs while closing the connection for any reason.
    I don't get it, any suggestions?
    Source:
    Code:java
    1. public static void kick(Player sender, String target, String message){
    2. String PlayerToBeKicked = target;
    3. String KickReason = message;
    4. OfflinePlayer receiver = plugin.getServer().getOfflinePlayer(target);
    5.  
    6.  
    7. plugin.sql.openConnection();
    8. if (receiver.isOnline()) {
    9.  
    10. plugin.sql.queryUpdate("INSERT INTO cr_kicks (player, isKickedFrom, reason) VALUES ('" + PlayerToBeKicked + "', '" + sender.getName() + "', '" + KickReason + "')");
    11. ((Player) receiver).kickPlayer("§6Du wurdest vom Server gekickt!\n§6Grund: §a" + KickReason);
    12. plugin.getServer().broadcastMessage("§3[§6Info§3]§6 " + receiver.getName() + " §ewurde von §6" + sender.getDisplayName() + " §egekickt!");
    13. plugin.getServer().broadcastMessage("§3[§6Info§3]§6 Grund: §e" + KickReason);
    14.  
    15.  
    16. } else {
    17. sysFunc.msg(sender,"Dieser Spieler ist nicht online!");
    18.  
    19. return;
    20. }
    21. plugin.sql.closeConnection();
    22. }


    Error:
    Code:
    [17:45:56] [Server thread/INFO]: Temphis issued server command: /kick Tera9411 huehuehue
    [17:45:56] [Server thread/INFO]: Tera9411 lost connection: §6Du wurdest vom Server gekickt!
    §6Grund: §ahuehuehue
    [17:45:56] [Server thread/INFO]: Tera9411 hat den Server verlassen!
    [17:45:56] [Server thread/INFO]: [0;36;22m[[0;33;22mInfo[0;36;22m][0;33;22m Tera9411 [0;33;1mwurde von [0;33;22mTemphis [0;33;1mgekickt![m
    [17:45:56] [Server thread/INFO]: [0;36;22m[[0;33;22mInfo[0;36;22m][0;33;22m Grund: [0;33;1mhuehuehue [m
    [17:45:56] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'kick' in plugin CopsRobbersSystem v0.0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.NullPointerException
        at me.Temphis.sys.SysDB.closeConnection(SysDB.java:138) ~[?:?]
        at me.Temphis.sys.teamFunc.kick(teamFunc.java:38) ~[?:?]
        at me.Temphis.sys.KickPlayerCommand.onCommand(KickPlayerCommand.java:20) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[srv.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        ... 13 more
    This is what crashs ( this.conn.close(); )
    Code:java
    1. public void closeConnection() {
    2. try {
    3. this.conn.close();
    4.  
    5. } catch (SQLException e) {
    6. // TODO Auto-generated catch block
    7. e.printStackTrace();
    8. } finally {
    9. this.conn = null;
    10. }
    11. }


    plugin.sql.closeConnection();
    That is line 38 on teamFunc.java, I wont post the full code, this is the entire function, its just that problem.
    Everything else works fine without problems, except that. He inserts the data into the database but he cant close the connection for any reason.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Wizehh

    If you aren't going to post your full code, I'm not going to bother helping you - but I will tell you this: don't set your 'conn' variable to null.
     
    mythbusterma likes this.
  3. Offline

    mythbusterma

    Dealyise

    Being stingy with your code and asking for help is not going to get you anywhere.

    You're setting your "conn" variable to null at some point in your code and then calling close() on it, which generates a null pointer exception. Not that hard.

    Also, do not run SQL queries from the main thread, a remote server can take and arbitrarily long time to respond to your query and that could cause severe lag on the server.
     
  4. Offline

    Dealyise

    Okay, sorry for that guys, but I just don't want copy cats here which copy my entire code then.
    Anyways, it worked, I removed this.conn = null; and it worked.
    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page