I cannot find what's wrong..

Discussion in 'Plugin Development' started by Condolent, May 4, 2016.

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

    Condolent

    So I get this stacktrace-error:
    Code:
    [20:10:41 ERROR]: Error occurred while enabling eBot v0.1a (Is it up to date?)
    java.lang.NullPointerException
            at net.eBot.commandExecs.ebotCmd.<init>(ebotCmd.java:19) ~[?:?]
            at net.eBot.main.eBotMain.onEnable(eBotMain.java:28) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:291) ~[craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.loadPlugin(CraftServer.java:346) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.enablePlugins(CraftServer.java:318) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at net.minecraft.server.v1_9_R1.MinecraftServer.t(MinecraftServer.java:403) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at net.minecraft.server.v1_9_R1.MinecraftServer.l(MinecraftServer.java:368) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at net.minecraft.server.v1_9_R1.MinecraftServer.a(MinecraftServer.java:323) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at net.minecraft.server.v1_9_R1.DedicatedServer.init(DedicatedServer.java:241) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit-1.9.jar:git-Bukkit-60f01ef]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    But I look at line 19 and can't see anything wrong.. Here's my main class it's referring to:
    Code:
    package net.eBot.main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.eBot.commandExecs.ebotCmd;
    import net.eBot.listeners.ConnectionListener;
    
    public class eBotMain extends JavaPlugin {
       
        public void onEnable() {
           
            // Message typed into the server console if the plugin has launched correctly
            getLogger().info("eBot enabled!");
           
            // Register eventlisteners
            this.getServer().getPluginManager().registerEvents(new ConnectionListener(this), this);
           
            // Generates config.yml
            getConfig().options().copyDefaults(true);
            saveConfig();
           
            // Command Executors
            getCommand("ebot").setExecutor(new ebotCmd(this));
           
        }
       
        public void onDisable() {
           
            getLogger().info("eBot disabled..");
           
        }
       
        public PluginDescriptionFile pdf = getDescription();
    
        public static String eBotPrefix = "[" + ChatColor.GREEN + "e" + ChatColor.DARK_GREEN + "Bot" + ChatColor.WHITE + "] ";
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
           
           
            if(cmd.getName().equalsIgnoreCase("test")) {
               
                p.sendMessage(eBotPrefix + "Plugin seems to work fine!");
               
                return true;
            }
           
           
            return false;
        }
    
    }
    
    This is the command-exec class:
    Code:
    package net.eBot.commandExecs;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    
    import net.eBot.main.eBotMain;
    
    public class ebotCmd implements CommandExecutor {
    
        eBotMain plugin;
       
        public ebotCmd(eBotMain instance) {
            plugin = instance;
        }
       
        PluginDescriptionFile pdf = plugin.getDescription();
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            String prefix = eBotMain.eBotPrefix;
           
            if(cmd.getName().equalsIgnoreCase("ebot")) {
               
                if(args.length < 1) {
                    p.sendMessage(prefix + "This server is running eBot version " + pdf.getVersion());
                    p.sendMessage(prefix + "For a command list, type /ebot help");
                } else if(args.length == 1) {
                    if(args[0].equalsIgnoreCase("help")) {
                        // TODO: Commands-list
                    }
                }
               
            }
           
            return false;
        }
    
    }
    
     
  2. Offline

    Lordloss

    For me it seems like the plugin field is null at the point pdf gets assigned. I would try to move the assigning of pdf into the constructor after the plugin = instance;
     
    MisterErwin likes this.
  3. Offline

    Condolent

    Code:
    public ebotCmd(eBotMain instance) {
            plugin = instance;
            PluginDescriptionFile pdf = plugin.getDescription();
        }
    If that is what you meant, it means that I cannot use the pdf :p (This is inside the commandexecutor-class

    EDIT: Seems like the pdf-line had to be inside the boolean for commands, the more you know! :) (Basically what you said when I read your post a bit closer, hehe)
     
    Last edited: May 4, 2016
    Lordloss likes this.
  4. Offline

    Lordloss

    Yes, i meant the
    Code:
    PluginDescriptionFile pdf;
    Outside of any methods (so as field)
    And inside the constructor
    Code:
    pdf = plugin.getDescription();
    If you dont fully understand whats the difference, you should google a bit about variable scope.
    Im glad if i helped you out, though.
     
  5. Offline

    mcdorli

    That's not a boolean, it's a method, wich returns obe.

    Don't watch theBCBroz
     
Thread Status:
Not open for further replies.

Share This Page