Method invocation 'setExecutor' may produce 'NullPointerException'

Discussion in 'Plugin Development' started by Brlesskoin, Mar 25, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Brlesskoin

    Hello, sorry for asking this but I'm starting to learn how to develop minecraft plugins and I've been following some tutorials over youtube, unfortunately most of them are outdated so... I'm here...

    Ok my problem is that the plugin is throwing Method invocation 'setExecutor' may produce 'NullPointerException' and it reffers to the line:

    getCommand("brtp").setExecutor(new Teleport());

    Console ERROR:

    [12:23:20] [Server thread/ERROR]: Error occurred while enabling FairTPA v1.0-SNAPSHOT (Is it up to date?)
    java.lang.NullPointerException: null


    I've been looking for a fix over the internet but all outdated... I'd be very thanksful for a little hand here guys, thank you.

    My full code of Main.java:

    Code:
    package me.brlesskoin.fairtpa;[/COLOR][/SIZE][/FONT][/B][/I]
    [FONT=Arial][SIZE=4][COLOR=#808080]
    [I][B]
    import me.brlesskoin.fairtpa.commands.Teleport;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Main extends JavaPlugin {
    
        public static final String TEXT_YELLOW;
    
        static {
            TEXT_YELLOW = "\u001B[33m";
        }
    
        public static final String RESET;
    
        static {
            RESET = "\033[0m";
        }
    
        @Override
        public void onEnable() {
            System.out.println(TEXT_YELLOW + "=============================================" + RESET);
            System.out.println(TEXT_YELLOW + "= " + RESET + "Fair Teleport ha sido activado" + TEXT_YELLOW + " =" + RESET);
            System.out.println(TEXT_YELLOW + "=============================================" + RESET);
    
            getCommand("brtp").setExecutor(new Teleport());
        }
    }
    

    My full code of Teleport.java:

    Code:
    package me.brlesskoin.fairtpa.commands;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Teleport implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            if (sender instanceof Player){
    
                Player player = (Player) sender;
    
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Debes especificar a quien iras a teleportarte.");
                    player.sendMessage(ChatColor.YELLOW + "El uso correcto es /brtp [NOMBRE]");
                } else if (args.length == 1) {
                    Player target = Bukkit.getPlayer(args[0]);
    
                    player.teleport(target.getLocation());
                }
    
            }
            return true;
        }
    }
    
    My full code of plugin.yml:

    Code:
    name: FairTPA
    version: 1.0 BETA
    author: Brlesskoin
    main: com.brlesskoin.fairtpa.main
    api-version: 1.18.2
    commands:
      brtp:
        description: Teleports player
        usage: /<command>
     
    Last edited: Mar 25, 2022
  2. Offline

    timtower Administrator Administrator Moderator

    @Brlesskoin getCommand may return null if it doesn't match one set in the plugin.yml
     
  3. Offline

    Brlesskoin

    Is exactly the same in both files command name "brtp" take a look to the codes I've posted and the error is being thrown by "setExecutor"
     
  4. Offline

    CraftCreeper6

  5. Offline

    timtower Administrator Administrator Moderator

    @Brlesskoin The error wasn't there yet.
    Post the full error code.
    And no need for all the formatting.
     
  6. Offline

    Brlesskoin

    This is the full console error guys

    Code:
    [12:46:22] [Server thread/ERROR]: Error occurred while enabling FairTPA v1.0-SNAPSHOT (Is it up to date?)
    java.lang.NullPointerException: Cannot invoke "org.bukkit.command.PluginCommand.setExecutor(org.bukkit.command.CommandExecutor)" because the return value of "me.brlesskoin.fairtpa.Main.getCommand(String)" is null
            at me.brlesskoin.fairtpa.Main.onEnable(Main.java:26) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.craftbukkit.v1_18_R1.CraftServer.enablePlugin(CraftServer.java:521) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at org.bukkit.craftbukkit.v1_18_R1.CraftServer.enablePlugins(CraftServer.java:435) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at net.minecraft.server.MinecraftServer.loadWorld0(MinecraftServer.java:612) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at net.minecraft.server.MinecraftServer.loadLevel(MinecraftServer.java:414) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:262) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:994) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.1-R0.1-SNAPSHOT.jar:3443-Spigot-699290c-2c1e499]
            at java.lang.Thread.run(Thread.java:833) [?:?]
    Seems that the problem comes from the Teleport.java class, it shows the warning

    Not annotated parameter overrides @NotNull parameter in line

    @Overridepublic boolean onCommand(CommandSender sender, Command command, String label, String[] args)
     
  7. Offline

    timtower Administrator Administrator Moderator

    @Brlesskoin Please show a screenshot of your project layout, getting the impression that the plugin.yml you posted is a different one from the one being used in the server
     
  8. Offline

    Brlesskoin

    [​IMG]I'm using IntelliJ and the structure was providen by the plugin of the IDE for making Spigot projects
     

    Attached Files:

  9. Offline

    timtower Administrator Administrator Moderator

    @Brlesskoin What is in that resources folder?
    And please don't log enable / disable messages, server does that for you already
     
  10. Offline

    Brlesskoin

    Well there was ANOTHER copy of plugin.yml... dunno why, I didn't even created none of both, I've edited it to match everything but the error still remains...

    Mod please close the thread...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 25, 2022
  11. Offline

    Strahan

    Check the plugin.yml that is in the actual exported jar to see if it is right. Also

    Code:
    public static final String TEXT_YELLOW;
    
    static {
    TEXT_YELLOW = "\u001B[33m";
    }
    
    public static final String RESET;
    
    static {
    RESET = "\033[0m";
    }
    Why are you setting the value like that? If you want to make a string constant, just do like
    Code:
    public static final String VAR = "blah";
    Also
    Code:
    Player target = Bukkit.getPlayer(args[0]);
    
    player.teleport(target.getLocation());
    Don't assume the person entered a valid player. Always assume any data coming in is invalid and act accordingly. e.g.
    Code:
    Player target = Bukkit.getPlayer(args[0]);
    if (target == null) {
      player.sendMessage(ChatColor.RED + "That isn't a valid player!");
      return true;
    }
    
    player.teleport(target.getLocation());
     
  12. Offline

    Brlesskoin

    Thank you, I'll fix those and keep trying :)
     
Thread Status:
Not open for further replies.

Share This Page