Why do I get an error?

Discussion in 'Spigot Plugin Development' started by Halcyon_, May 12, 2016.

  1. Title explains it all. Here's my code
    Code (Text):
    package clock.clock;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;

    import clock.clock.EventHandle;

    public class Clock implements Listener {

        ArrayList<Player> freeze = new ArrayList<Player>();

        private EventHandle plugin;

        public Clock(EventHandle plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }

        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {

            Player p = e.getPlayer();

            if ((this.freeze.contains(p)) && (

            (e.getFrom().getX() != e.getTo().getX()) || (e.getFrom().getZ() != e.getTo().getZ()))) {

                e.setTo(e.getFrom());

                return;
            }
        }

        @EventHandler
        public void clockClick(PlayerInteractEvent e) {

            Player player = e.getPlayer();

            if (((e.getAction() == Action.LEFT_CLICK_AIR) || (e.getAction() == Action.LEFT_CLICK_BLOCK))
                    && (player.getItemInHand().getType() == Material.WATCH) && player.getGameMode() == GameMode.SURVIVAL) {

                int amount = player.getPlayer().getItemInHand().getAmount();
                player.getItemInHand().setAmount(amount - 1);
                if (player.getItemInHand().getAmount() == 1) {

                    e.getPlayer().getInventory().setItemInHand(new ItemStack(Material.AIR, 1));

                }
                double blockDistance = 30;
                for (Player other : Bukkit.getOnlinePlayers()) {
                    if (other.getLocation().distance(player.getLocation()) <= blockDistance && other != player) {
                        freeze.add(other);
                        Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
                            public void run() {
                                freeze.remove(other);
                            }
                        }, 200);
                    }
                }
            }
        }
    }
    Here is my log
    HTML:
    [18:45:13 ERROR]: Could not pass event PlayerInteractEvent to TestClock v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1200) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:397) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:123) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:106) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at clock.clock.Clock.clockClick(Clock.java:62) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            ... 18 more
    [18:45:13 ERROR]: Could not pass event PlayerInteractEvent to TestClock v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1200) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:397) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:123) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:106) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at clock.clock.Clock.clockClick(Clock.java:62) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            ... 18 more
    [18:45:13 ERROR]: Could not pass event PlayerInteractEvent to TestClock v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1200) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:397) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:123) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:106) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at clock.clock.Clock.clockClick(Clock.java:62) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            ... 18 more
     
  2. You never initialized your plugin variable. So I'm guessing when you try to create this scheduler with the "plugin" variable, it won't work since plugin is null.
     
    • Agree Agree x 1
  3. How would I fix that?
     
  4. In your main class, the one that extends java plugin, you can initialize your plugin by doing something like...
    plugin = this;
    Or you could make a method to return the instance of your main class.
     
  5. I already have this
    Code (Text):
    private EventHandle plugin;

        public Clock(EventHandle plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
  6. Can you help me?