Solved Throwing an NPE?

Discussion in 'Plugin Development' started by RoboticPlayer, Oct 4, 2015.

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

    RoboticPlayer

    For some reason, an NPE keeps getting thrown whenever I try to run a command that I am trying to update from one of my plugins.
    I am trying to run the command /ag (alias for /admingui). Here is the error that I get:
    NPE Error (open)

    Code:
    [20:16:46 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ag' in plugin SuperAdmin v1.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_60]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
            at me.roboticplayer.superadmin.commands.AdminGUI.onCommand(AdminGUI.java:39) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            ... 15 more

    Here is my AdminGUI class:
    Code:
    package me.roboticplayer.superadmin.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.roboticplayer.superadmin.SuperAdmin;
    import me.roboticplayer.superadmin.inventories.MainGUI;
    
    public class AdminGUI implements CommandExecutor {
    
        private MainGUI gui;
    
        public AdminGUI(MainGUI instance) {
            gui = instance;
        }
    
        @SuppressWarnings("unused")
        private SuperAdmin plugin;
    
        public AdminGUI(SuperAdmin plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("admingui")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "The console cannot use this command!");
                    return true;
                }
    
                if (!(sender.hasPermission("superadmin.gui"))) {
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to perform this command!");
                    return true;
                }
                ((Player) sender).openInventory(gui.mainGUI);
                // plugin.openGUI((Player) sender);
            }
            return true;
        }
    }
    
    And my MainGUI class:
    Code:
    package me.roboticplayer.superadmin.inventories;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class MainGUI {
    
        public Inventory mainGUI;
    
        public ItemStack chat;
        public ItemStack world;
        public ItemStack other;
        public ItemStack inventory;
        public ItemStack player;
    
        public void chatItem() {
            chat = new ItemStack(Material.BOOK_AND_QUILL);
        }
    
        public void worldItem() {
            world = new ItemStack(Material.EMPTY_MAP);
        }
    
        public void otherItem() {
            other = new ItemStack(Material.BUCKET);
        }
    
        public void inventoryItem() {
            inventory = new ItemStack(Material.DIAMOND_CHESTPLATE);
        }
    
        public void playerItem() {
            player = new ItemStack(Material.MINECART);
        }
    
        public MainGUI() {
            mainGUI = Bukkit.getServer().createInventory(null, 9, ChatColor.DARK_RED + "" + ChatColor.BOLD + "Admin GUI");
    
            mainGUI.setItem(0, chat);
            mainGUI.setItem(2, world);
            mainGUI.setItem(4, other);
            mainGUI.setItem(6, inventory);
            mainGUI.setItem(8, player);
        }
    }
    
    I don't exactly know why it's throwing an NPE (yes, I know that it's on line 39 of AdminGUI.java), can someone help? @teej107 @mythbusterma
     
  2. Offline

    Xerox262

    Show your JavaPlugin class too, the MainGUI being passed to the command is most likely null or it's because you're setting the inventory to items that are null
    Code:
    public ItemStack chat;
        public ItemStack world;
        public ItemStack other;
        public ItemStack inventory;
        public ItemStack player;
    
    You assign them in methods, but you never call those methods
     
  3. Offline

    RoboticPlayer

    @Xerox262 I tried changing it so that I am calling the methods, but it returned the same error
    Code:
    package me.roboticplayer.superadmin.inventories;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class MainGUI {
    
        public Inventory mainGUI;
    
        public ItemStack chat;
        public ItemStack world;
        public ItemStack other;
        public ItemStack inventory;
        public ItemStack player;
    
        public ItemStack chatItem() {
            chat = new ItemStack(Material.BOOK_AND_QUILL);
    
            return chat;
        }
    
        public ItemStack worldItem() {
            world = new ItemStack(Material.EMPTY_MAP);
    
            return world;
        }
    
        public ItemStack otherItem() {
            other = new ItemStack(Material.BUCKET);
    
            return other;
        }
    
        public ItemStack inventoryItem() {
            inventory = new ItemStack(Material.DIAMOND_CHESTPLATE);
    
            return inventory;
        }
    
        public ItemStack playerItem() {
            player = new ItemStack(Material.MINECART);
    
            return player;
        }
    
        public MainGUI() {
            mainGUI = Bukkit.getServer().createInventory(null, 9, ChatColor.DARK_RED + "" + ChatColor.BOLD + "Admin GUI");
    
            mainGUI.setItem(0, chatItem());
            mainGUI.setItem(2, worldItem());
            mainGUI.setItem(4, otherItem());
            mainGUI.setItem(6, inventoryItem());
            mainGUI.setItem(8, playerItem());
        }
    }
    
     
  4. Offline

    teej107

    @henderry2019
    1. Don't publicly expose your fields like that. Use encapsulation!
    2. Why do you have 2 completely different constructors in your AdminGUI?
     
  5. Offline

    RoboticPlayer

    The one using the SuperAdmin class is what I was using previously, and I want to still have it if what I'm trying to do doesn't work out.

    Edit: And why is it better to use encapsulation?
     
  6. Offline

    teej107

    https://dzone.com/articles/why-encapsulation-matters

    Problems that arise do to a lack of encapsulation can be a nightmare to find. Encapsulation makes code easy to read, understand, and to prevent problems.
     
  7. Offline

    Xerox262

    You ignored the other thing I said, You never showed your JavaPlugin class. Odds are you're passing the plugin constructor and not the MainGui constructor, which means the gui field will be null
     
  8. Offline

    RoboticPlayer

    In my JavaPlugin class I have no references to the MainGUI class at all. I am not able to show it at the moment as I am not at my computer.
     
  9. Offline

    Xerox262

    That's what I'm talking about, in your onEnable you probably have getCommand(commandName).setExecutor(new AdminGUI(this)); when it should be getCommand(commandName).setExecutor(new AdminGUI(MainGUI Object));
     
  10. Offline

    RoboticPlayer

    Oh, I see what you mean. @Xerox262 I'll try to fix that once I get back.

    @Xerox262 Tried that, didn't change anything.

    Code:
        private MainGUI instance;
    
        public SuperAdmin(MainGUI instance) {
            this.instance = instance;
        }
    
        public void registerCommands() {
            this.getCommand("admintoggle").setExecutor(new AdminToggle(this));
            this.getCommand("admingui").setExecutor(new AdminGUI(instance));
        }
    Error (open)

    Code:
    [20:34:57 ERROR]: Could not load 'plugins\SuperAdmin.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Abnormal plugin type
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:56) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:289) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.<init>(CraftServer.java:251) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.PlayerList.<init>(PlayerList.java:69) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:179) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:504) [craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.InstantiationException: me.roboticplayer.superadmin.SuperAdmin
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            ... 10 more
    Caused by: java.lang.NoSuchMethodException: me.roboticplayer.superadmin.SuperAdmin.<init>()
            at java.lang.Class.getConstructor0(Unknown Source) ~[?:1.8.0_60]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit-1.8.8.jar:git-Bukkit-62a2169]
            ... 10 more

    @teej107 @mythbusterma

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  11. Offline

    mythbusterma

    @henderry2019

    You created a non-default constructor for your JavaPlugin class, don't do that.
     
  12. Offline

    RoboticPlayer

    @mythbusterma Then how would I go about doing this properly? I've never tried to use methods from a different class for a command.
     
  13. Offline

    mythbusterma

    @henderry2019

    Just don't declare a non-default constructor (one that has arguments). Just don't do it. That's all you have to do.
     
  14. Offline

    RoboticPlayer

    Solved. I just got rid of the constructor in my JavaPlugin class and instead did this:
    Code:
    getCommand("admingui").setExecutor(new AdminGUI(new MainGUI());
     
Thread Status:
Not open for further replies.

Share This Page