Solved NullPointerException - Multiple classes

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

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

    bombom3000

    Hello!
    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:
    Code:java
    1. import org.bukkit.Material;
    2. import org.bukkit.inventory.ItemStack;
    3.  
    4. public class Menus {
    5.  
    6. public Main main;
    7. public static Menus instance;
    8.  
    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. }
    20.  

    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:
    Code:java
    1. public Menus menu;
    2.  
    3. onCommand() Here {
    4. this.menu = 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,

    Charlie
     
  2. Offline

    Forseth11

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

    teej107

  4. Offline

    bombom3000


    Oh yes sorry! I knew there was something I'd forgotten!
    Code:
    [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(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [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(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
    Caused by: java.lang.NullPointerException
        at com.quake.main.Commands.onCommand(Commands.java:40) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-2-g1e4dcde-b3086jnks]
        ... 13 more
    
    Just so you know, line 40 in Commands.java is the menu.settingsMenu().open(player); in the 2nd code block above.

    Charlie
     
  5. Offline

    ampayne2

    bombom3000

    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

    bombom3000

    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!

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

Share This Page