0

Can't understand how to fix that error, it's unreal to me (can't find where's void in my code):

java.lang.NullPointerException: null
    at survival.MainClass.onEnable(MainClass.java:16) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]

16 line is:

getCommand(commands.cmd3).setExecutor(commands);

Commands class:

public class Commands extends CommandExecute implements CommandExecutor, Listener {

public String cmd1 = "fly";
public String cmd2 = "feed";
public String cmd3 = "heal";

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
        if (cmd.getName().equalsIgnoreCase(cmd3)) {
            Player p = (Player) sender;
            if (p.getHealth() < 20) {
                p.setHealth(20);
                p.sendMessage(ChatColor.GOLD + "Healed succesfuly!");
                return true;
            } else {
                p.sendMessage(ChatColor.RED + "You are already healed!");
                return true;
            }

        }
        return false;
    } else {
        sender.sendMessage("Only for Players!");
        return true;
    }

  }

}

MainClass:

public class MainClass extends JavaPlugin implements Listener{
private Commands commands = new Commands();

public void onEnable() {
    getServer().getPluginManager().registerEvents(new MainHandler(), this);
    getCommand(commands.cmd1).setExecutor(commands);
    getCommand(commands.cmd2).setExecutor(commands);
    getCommand(commands.cmd3).setExecutor(commands);
  }
}

Have cmd1 and cmd2 before that and they're working fine, problem with only cmd3 command.

in plugin.yml:

name: Survival
main: survival.MainClass
version: 1.1
commands:
  fly:
    description: Toggle flight mode
    usage: /fly
  feed:
    description: Feeds player
    usage: /feed
  heal:
    description: Heals player
    usage: /heal

Tried all answers on stackoverflow, but nothing helped me

vladis086
  • 41
  • 1
  • 6
  • Well, obviously this happens at some line in `MainClass.java`. Hard to tell which is line 16, but it probably is in `onEnable()`. Have you already tried to find out what `getServer()` and `getServer().getPluginManager()` return? And, if that succeeds, check `getCommand(commands.cmd1)` etc.? – glglgl Jan 06 '19 at 17:19
  • You need to add all commands to your `plugin.yml`, not just the `heal` command! – Jacob G. Jan 06 '19 at 17:25
  • What is on line 16 in `MainClass.java` – Rockey Jan 06 '19 at 17:51
  • @Rockey 16 line is: getCommand(commands.cmd3).setExecutor(commands); – vladis086 Jan 06 '19 at 18:04
  • Check if `getCommand(commands.cmd3)` returns `null` – Rockey Jan 06 '19 at 18:09

1 Answers1

2

I finally did it!

I changed

private Commands commands = new Commands();

to

private Commands commands;

and set value for commands variable after onEnable() and it's looking now:

public void onEnable() {
    commands = new Commands();
    getServer().getPluginManager().registerEvents(new MainHandler(), this);
    getCommand(commands.cmd1).setExecutor(commands);
    getCommand(commands.cmd2).setExecutor(commands);
    getCommand(commands.cmd3).setExecutor(commands);
vladis086
  • 41
  • 1
  • 6