[Solved] NullPointerException ????

Discussion in 'Spigot Plugin Help' started by RayoxHD, May 12, 2015.

  1. Error
    Code (Text):
    [22:49:19] [Server thread/INFO]: RayoxHD issued server command: /setspawn
    [22:49:19] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setspawn' in plugin WorldSpawns v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-eb39b47-0899683]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-eb39b47-0899683]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-eb39b47-0899683]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot.jar:git-Spigot-eb39b47-0899683]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot.jar:git-Spigot-eb39b47-0899683]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
        at me.rayoxhd.worldspawn.commands.SetSpawnCommand.onCommand(SetSpawnCommand.java:19) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-eb39b47-0899683]
        ... 14 more
    SetSpawnComannd.java

    Code (Text):
    package me.rayoxhd.worldspawn.commands;

    import java.io.IOException;

    import me.rayoxhd.worldspawn.Main;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class SetSpawnCommand implements CommandExecutor {
       
        Main m = null;
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player)sender;
           
            m.spawndataConfig.set(p.getWorld().getName()+".X", p.getLocation().getX());
            m.spawndataConfig.set(p.getWorld().getName()+".Z", p.getLocation().getZ());
            m.spawndataConfig.set(p.getWorld().getName()+".Y", p.getLocation().getY());
            try {
                m.spawndataConfig.save(m.spawndata);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }

    }  
     
    Line 19 is :

    m.spawndataConfig.set(p.getWorld().getName()+".X", p.getLocation().getX());
     
  2. 'M' is null, and you set it to null at the top of the class. That's causing the NPE to be thrown.
     
  3. but it is thrown even if i just do Main m;
     
  4. That's because a variable which has been initialized but no value set still equals to null. If you are trying to access your config from the main class, then do the following:

    Add this to your main class:

    Code (Text):
    private static <Main Class Name> instance;

    public static <Main Class Name> getInstance(){
            return instance;
        }
    And of course, replace <Main Class Name> with the name of your main class.

    Last but not least, add this to your onEnable():
    Code (Text):
    instance = this;
    Then, all you need to do to access the config is do <Main Class Name>.getInstance().spawndataConfig().<what you want to do with the config>.
     
    • Like Like x 1
  5. Thank you @omon23 Very helpful. Sloved my Problem