Solved NullPointerException - Multiple classes

Discussion in 'Plugin Development' started by bombom3000, Aug 9, 2014.

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


    Seeing as you answered my question about a null pointer exception while referencing my 'Main' class very well I was wondering if you could help with another NullPointerException I am getting.
    I am using this class to create icon menus, it works perfectly except I have gone ahead and created a 'Menus' class to store my different menus in:
    1. import org.bukkit.Material;
    2. import org.bukkit.inventory.ItemStack;
    4. public class Menus {
    6. public Main main;
    7. public static Menus instance;
    9. public IconMenu settingsMenu() {
    10. IconMenu menu = new IconMenu("My Fancy Menu", 9, new IconMenu.OptionClickEventHandler() {
    11. @Override
    12. public void onOptionClick(IconMenu.OptionClickEvent event) {
    13. event.getPlayer().sendMessage("You have chosen " + event.getName());
    14. event.setWillClose(true);
    15. }
    16. }, Main.getPlugin(Main.class)).setOption(3, new ItemStack(Material.APPLE, 1), "Food", "The food is delicious").setOption(4, new ItemStack(Material.IRON_SWORD, 1), "Weapon", "Weapons are for awesome people").setOption(5, new ItemStack(Material.EMERALD, 1), "Money", "Money brings happiness");
    17. return menu;
    18. }
    19. }

    This 'Menus' class contains example code from the thread, which does work when I don't try to run it from another class.
    I have tried making an instance of this class and opening it with a command by using in my Commands class:
    1. public Menus menu;
    3. onCommand() Here {
    4. = Menus.instance;
    5. Player player = (Player) sender;
    6. command here {
    7. menu.settingsMenu().open(player);
    8. }
    9. }

    Please could you help me get rid of this exception, whatever I've tried hasn't worked and I've tried to follow these instructions which solved another NPE, but changed the names. Nothing I've found on the internet has worked.

    Let me know if I have missed something, or want me to clarify.

    Thank you for your time,

  2. Offline


    Can you post the stack trace. It is so much easier to find the problem when I can see the stack trace.
  3. Offline


  4. Offline


    Oh yes sorry! I knew there was something I'd forgotten!
    [20:46:18 INFO]: bombom3000 issued server command: /qc stop
    [20:46:18 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'qc' in plugin QuakeCraftDeluxe v0.1
        at org.bukkit.command.PluginCommand.execute( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u( [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
    Caused by: java.lang.NullPointerException
        at com.quake.main.Commands.onCommand( ~[?:?]
        at org.bukkit.command.PluginCommand.execute( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        ... 13 more
    Just so you know, line 40 in is the menu.settingsMenu().open(player); in the 2nd code block above.

  5. Offline



    Your Menus.instance variable is null - to fix this you could either remove the variable and just make the settingsMenu() method static (slightly better way of doing it), or actually give the instance variable an instance.
  6. Offline


    ampayne2 Thank you! That will help me in the future, I still haven't gotten the hang of those identifiers after doing research on the internet.

    Thanks again!

    ampayne2 likes this.
Thread Status:
Not open for further replies.

Share This Page