mysql nullpointerexception

Discussion in 'Spigot Plugin Development' started by robotnik, May 30, 2016.

  1. When I try to execute a command for my plugin, I get a NPE

    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'blockmessage' in plugin TalkingBlocks v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
        at TalkingBlocks.BlockCommand.onCommand(BlockCommand.java:39) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        ... 15 more

    Here is my main class

    Code (Text):
    public class TalkingBlocks extends JavaPlugin {

        private File configf, databasefile;
        private FileConfiguration config, special;
       
        private TalkingBlocks plugin;
       
        private Connection connection;
        String host, database, username, password;
        private int port;

        static Statement dbstatement;
       
        @Override
        public void onEnable(){
           
            //register commands
            this.getCommand("blockmessage").setExecutor(new BlockCommand(plugin));

           
            try {
                createFiles();
            } catch (InvalidConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
            //database config information
            host = config.getString("host", "localhost");
            port = config.getInt("port", 3306);
            database = config.getString("database_name", "TestDatabase");
            username = config.getString("username", "user");
            password = config.getString("password", "pass");
           
           
            try {  
                openConnection();
                dbstatement = connection.createStatement();        
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
       

       

        public FileConfiguration getDatabaseConfig() {
            return this.special;
        }


        public void openConnection() throws SQLException, ClassNotFoundException {
        if (connection != null && !connection.isClosed()) {
            return;
        }

        synchronized (this) {
            if (connection != null && !connection.isClosed()) {
                return;
            }
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
            }  
        }
       
        public Statement GetStatement(){
            return dbstatement;
           
        }
       
        private void createFiles() throws InvalidConfigurationException {

            configf = new File(getDataFolder(), "config.yml");


            if (!configf.exists()) {
                configf.getParentFile().mkdirs();
                saveResource("config.yml", false);
            }
           


            config = new YamlConfiguration();

            try {
                config.load(configf);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    and the command class where the NPE occurs

    Code (Text):
    package TalkingBlocks;

    import java.io.IOException;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.HashSet;
    import java.util.Set;

    import net.minecraft.server.v1_9_R1.Material;

    import org.bukkit.Chunk;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.conversations.Prompt;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    public class BlockCommand implements CommandExecutor{
       //store blocks in a file for now, somehow
       
       private TalkingBlocks plugin;

      public BlockCommand(TalkingBlocks plugin){
         this.plugin = plugin;
      }  
       
       
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label,
           String[] args) {
         Player player = (Player) sender;
         
         //NOTE: use .equals() when comparing strings in java
         
         //set message command
      if (sender instanceof Player) {
        Statement dbstatement = plugin.GetStatement(); //line 39
         
        Block lookingatblock = player.getTargetBlock(((HashSet<Byte>) null), 30);
         
        Chunk blockchunk = lookingatblock.getChunk();
         
        int chunkx = blockchunk.getX();
        int chunkz = blockchunk.getZ();
         
        String message = args[0];
         
        int x = lookingatblock.getX();
        int y = lookingatblock.getY();
        int z = lookingatblock.getZ();

        try {
               dbstatement.executeUpdate("INSERT INTO " + plugin.database + "(PLAYERUUID, BLOCKNAME, MESSAGE, CHUNKX, CHUNKZ, X , Y , Z) VALUES (" + player.getUniqueId() + ", " + lookingatblock.getType().toString() + ", " + message + ", " + chunkx + ", " + chunkz + ", " + x + ", " + y + ", " + z + ");");
             } catch (SQLException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
             }
           return true;  
      }
         return false;
       }
    }
     

    Im not really sure what to do here, I dont see how dbstatement is null
     
  2. dbstatement isn't null. plugin is. Change this.getCommand("blockmessage").setExecutor(new BlockCommand(plugin)); to this.getCommand("blockmessage").setExecutor(new BlockCommand(this)); in your onEnable
     
    • Like Like x 1
  3. That fixed it, thanks!