Solved NullPointerException caused by Runnable()?

Discussion in 'Spigot Plugin Development' started by Moderance, May 27, 2016.

  1. So I made my own spawn command which works fine, but when I try and add a runnable (delay) to it, it seems to error, I have no idea what the issue is as there are no errors on eclipse either, here's the code:

    Code (Text):

    public static Core plugin;
    //On command code, etc..

    public static int delay = 3;
    public static void sendToSpawn(Player player) {
            File file = Core.getConfigFile();
            if (file.exists()) {
                YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
             
                String world = (String) config.get("spawn.world");
                double x = config.getDouble("spawn.x");
                double y = config.getDouble("spawn.y");
                double z = config.getDouble("spawn.z");
                int yaw = config.getInt("spawn.yaw");
                int pitch = config.getInt("spawn.pitch");
             
                plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    public void run() {
                        if (delay != -1) {
                            if (delay != 0) {
                                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Server ยป &7Teleporting you to &espawn &7in &a" + delay + "&7s" ));
                                delay--;
                            } else {
                                player.teleport(new Location(Bukkit.getServer().getWorld(world), x, y, z, (float) yaw, (float) pitch));
                                delay--;
                            }
                        }
                    }
                }, 0L, 20L);
                 
            } else {
                System.out.println("[Error] Could not find the location of config.yml!");
            }
        }
    I also have all of my commands in separate .class's.
     
    #1 Moderance, May 27, 2016
    Last edited: May 28, 2016
  2. Serializator

    Supporter

    Please post the stacktrace that's printed in your console.
     
  3. Take a look here that way you can learn how to debug the errors yourself :)
     
  4. Yes I know, I wouldn't be asking otherwise..

    Sorry, here it is:

    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin Server v1.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1350) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1185) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            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_R1.SystemUtils.a(SourceFile:45) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
            at moderance.commands.spawn.Spawn.sendToSpawn(Spawn.java:145) ~[?:?]
            at moderance.commands.spawn.Spawn.onCommand(Spawn.java:35) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-e6f93f4-ed60c01]
            ... 15 more
    Line 145:
    Code (Text):
    plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    Line 35:
    Code (Text):
    sendToSpawn(player);
     
  5. Serializator

    Supporter

    Make sure the variable "plugin" isn't null.
    Simply print the variable out in the console and see if it prints out "null".
     
    • Agree Agree x 1
  6. I don't know what is wrong with line 145, which is why I am asking this question, I know how to debug
     
  7. Ah... :s It's null, but I defined it at the start of that class:

    Code (Text):
    private static Core plugin;
     
  8. Serializator

    Supporter

    You've defined it, but did you initialize it in your `onEnable()`?
    I'm assuming the variable you've shown me is defined in the main class and not the class with the runnable.
     
  9. Yes, and you define it as null right there :p
     
    • Agree Agree x 1
  10. No everything I have shown is in the Spawn.class - Is there another way to get the plugin from another class?
     
  11. In your class:
    Code (Text):
    public Core plugin;

    public Spawn(Core plugin) {
      this.plugin = plugin;
    }
    In main class:
    Code (Text):
    public Spawn spawn;

    public void onEnable() {
      this.spawn = new Spawn(this);
    }
    Now you can get the plugin in Spawn.class.
     
  12. Serializator

    Supporter

    And use appropriate access modifiers, in this case "private" and probably "final" (not an access modifier).
     
    • Informative Informative x 1
    • Useful Useful x 1
  13. Doesn't work..?

    Eclipse tells me to make public Core plugin; to static and then change this. to Spawn.plugin and when I test it it prints plugin = null and same error.
     
  14. Core (main):
    http://pastebin.com/WebeMJ67

    Spawn:
    http://pastebin.com/ReEMuurc

    Getting errored on the spawn class at telling me to make it static but then it tells me to make this. to spawn. and then it still = null

    Code (Text):
                System.out.println(plugin);
             
                plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
     
  15. In Spawn.class:
    Code (Text):
    public static void sendToSpawn(Player player) {
    Change it to:
    Code (Text):
    public void sendToSpawn(Player player) {
    Instead of "Spawn.sendToSpawn(player)", you should use "spawn.sendToSpawn(player)"
     
  16. Or, if you want to use static modifier, insert this code in Core.class:
    Code (Text):
    public void onEnable() {
      Spawn.plugin = this;
    }
    And in Spawn.class:
    Code (Text):
    public static Core plugin;
    But I recommend the former one: not using static modifier.
     
  17. Do you have skype or something, this really isnt workoing and I have no idea why. Or can you pastebin what it should be..?
     
  18. If I am not mistaken I believe you can get rid of the plugin variable completely by changing this:
    Code (Text):

    plugin.getServer().getScheduler()
     
    to this:
    Code (Text):

    Bukkit.getScheduler()
     
     
    • Agree Agree x 1
  19. He still needs it to schedule a task.
     
  20. Lol, yeah true.
    I guess it would still be possible to remove it if you used something like "Bukkit.getPluginManager().getPlugin("PluginName")" but its kinda hacky IMO.
    This really is basic, basic Java... I suggest the OP brush up on Java/programming fundamentals before moving forward with the project.