Solved Chunk check

Discussion in 'Spigot Plugin Development' started by Deger, Jun 27, 2016.

  1. Hello i have a code that gives me a NPE, i am really bad at using a config, but why is it giving me a null when i have specified a int in my config?

    Code (Text):
    package me.deger.chunk.command;

    import org.bukkit.ChatColor;
    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.entity.Player;

    import me.deger.chunk.core.Core;

    public class ChunkCommand implements CommandExecutor {

        Core core;
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            int ammount = 0;
            String cmd = command.getName();

            if (cmd.equalsIgnoreCase("check")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Only players are allowed to use this command!");
                    return true;
                }
                Player p = (Player) sender;
                Chunk c = p.getLocation().getChunk();

                for (int x = 0; x <= 16; x++) {
                    for (int y = 0; y <= 256; y++) {
                        for (int z = 0; z <= 16; z++) {
                            Block b = c.getBlock(x, y, z);
                            if (b.getTypeId() == core.getConfig().getInt("Items.id"))
                                ammount++;
                        }
                    }
                }
                if (ammount == 0) {
                    p.sendMessage(ChatColor.GOLD + "There is " + ChatColor.GREEN + "NO" + ChatColor.GOLD
                            + " Diamonds Ores in this CHUNK");
                } else {
                    p.sendMessage(ChatColor.GOLD + "There is " + ChatColor.GREEN + ammount + ChatColor.GOLD
                            + " Diamonds Ores in this CHUNK");
                }
            }
            return false;
        }

    }
     

    Code (Text):
    package me.deger.chunk.core;

    import org.bukkit.plugin.java.JavaPlugin;

    import me.deger.chunk.command.ChunkCommand;

    public class Core extends JavaPlugin{

        @Override
        public void onEnable(){
            registerCommands();
            saveDefaultConfig();
        }


        @Override
        public void onDisable(){
       
        }

        public void registerCommands() {
            getCommand("check").setExecutor(new ChunkCommand());
       
        }
    }
     

    Code (Text):
    [19:28:53 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'check' in plugin Chunk v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
            at me.deger.chunk.command.ChunkCommand.onCommand(ChunkCommand.java:33) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            ... 15 more
     
    #1 Deger, Jun 27, 2016
    Last edited: Jun 27, 2016
  2. Check if "core.getConfig().getInt("Items.id")" is returning right integer, also you should declare this number as local variable and not always access to config in loop.
     
  3. Why not acces in a loop (the config)
     
  4. I have to acces it in my loop.... or can you give a better example of how to do it then?
    I fixed the null pointer!!
     
  5. Code (Text):

    int t = core.getConfig().getInt("Items.id");
    if(t == null){
    //do something to fix that. That should never happen i think that return 0 if config is not null.
    }
    for (int x = 0; x <= 16; x++) {
                    for (int y = 0; y <= 256; y++) {
                        for (int z = 0; z <= 16; z++) {
                            Block b = c.getBlock(x, y, z);
                            if (b.getTypeId() == t){
                                ammount++;
                           }
                        }
                    }
                }
     
  6. int can't be null, the problem is that the core variable is null.
     
    • Agree Agree x 1
  7. Im on my phone and may have missed it, but where are you initialising your core?

    Edit:
    Ninjad, wow took my 6mins to type that.....
     
  8. I already fixed everything, i just made a manager class and i made antohter yml (called spawn.yml) and added my Items.id and got it and it works like a charm!!
    I am not doing it anymore, im fixed it!