Solved Was trying to create an inventory/gui tool, getting a null pointer that i cant seem to figure out!

Discussion in 'Plugin Development' started by raunak114, Mar 23, 2017.

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

    raunak114

    Hey guys!
    I have a serious problem, I was trying to make a class(s) that make it easier for me to make a bukkit GUI (I know there might be libraries out there.. But I wanted to make my own!) :D...
    So after everything was configured, eclipse showed no error whatsoever, nor did creating one of my Objects of the class, the problem was executing, so basically there is a class I made called IconMenu, that is the class that has all the data of the object...
    I am a beginner so please excuse me, here is the error:-

    Code:
    [23:48:21] [Server thread/INFO]: raunak114 issued server command: /sgive
    [23:48:21] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'sgive' in plugin Skyblock_Commands v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:647) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1358) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1193) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
    at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
    at menuLib.MenuListener.isRegistered(MenuListener.java:47) ~[?:?]
    at menuLib.ItemMenu.open(ItemMenu.java:85) ~[?:?]
    at commands.Cgive.onCommand(Cgive.java:61) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
    ... 15 more
    [23:52:33] [Server thread/INFO]: raunak114 issued server command: /rl



    In the error, there are three classes involved, the MenuListener.isRegistered is a method in the menu listener class
    that is as follows:-
    Code:
    public boolean isRegistered(JavaPlugin plugin)
    {
      if (plugin.equals(this.plugin)) {
        for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
          if (listener.getListener().equals(INSTANCE)) {
            return true;
          }
        }
      }
      return false;
    }

    Here is the declaration of the two things, (Plugin and instance)
    Code:
    private static final MenuListener INSTANCE = new MenuListener();
    private Plugin plugin = null;
    The second error is in the ItemMenu.open method, which is as follows:-
    Code:
      {
        if (!MenuListener.getInstance().isRegistered(this.plugin)) {
          MenuListener.getInstance().register(this.plugin);
        }
        Inventory inventory = Bukkit.createInventory(new MenuHolder(this, Bukkit.createInventory(player, this.size.getSize())), this.size.getSize(), this.name);
        apply(inventory, player);
        player.openInventory(inventory);
      }


    The third one is the class Cgive, which is a command class, with the whole class being as follows:

    Code:
    package commands;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import AskyBlock.Items;
    import SBc.Mainclass;
    import library.CustomPlayer;
    import menuLib.ItemMenu;
    import menuLib.ItemMenu.Size;
    import menuLib.MenuItem;
    
    public class Cgive implements CommandExecutor{
    
    //public static Inventory spellgui = Bukkit.createInventory(null, 9, ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "Spell Merchant");
    Mainclass plugin;
    public Cgive(Mainclass plugin){
      this.plugin = plugin;
    }
    CustomPlayer pl;
    public Cgive(CustomPlayer pl){
      this.pl = pl;
    }
    Items item;
    public Cgive(Items item){
      this.item = item;
    }
    ItemMenu menu = new ItemMenu("", Size.ONE_LINE, plugin);
    @SuppressWarnings("unused")
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
      if (!(sender instanceof Player)){
       Bukkit.getLogger().severe("This command is only meant for Players!");
       return false;
      }
      Player p = (Player) sender;
      if(commandLabel.equalsIgnoreCase("Sgive")){
       menu.setItem(1, new MenuItem(Fpot20().getItemMeta().getDisplayName(), Fpot20(), Fpot20().getItemMeta().getLore().toArray(new String[Fpot20().getItemMeta().getLore().size()])));
       menu.open(p);
    p.sendMessage("tt");
    }
      return false;
    }
    public ItemStack Fpot20(){
    ItemStack Fpot20 = new ItemStack(Material.SLIME_BALL);
    ItemMeta Fmeta20 = Fpot20.getItemMeta();
    Fmeta20.setDisplayName(ChatColor.RED + "Potion of Flight");
    Fmeta20.setLore(Arrays.asList(ChatColor.GOLD.toString() +"Emerged from the Sky in the Ancient time",ChatColor.GOLD.toString() +"This elemental potion will let you",ChatColor.GOLD.toString() +"and your Island mates fly for a brief time" , ChatColor.RED.toString() + ChatColor.BOLD.toString() + "Effect time: " + ChatColor.GREEN.toString() + "20 minutes",ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "Cost: " + ChatColor.GREEN.toString() + "15000$"));
    
    Fpot20.setItemMeta(Fmeta20);
    Fpot20.addEnchantment(plugin.glow, 1);
    return Fpot20;
    }
    }
    Please tell me what my error is :D, I know I can count on you guys!

    Much love,
    Raunak114

    Also I am registering the events on my on enable with these:

    Code:
    menuLib.MenuListener.getInstance().register(this);
    Code:
    public void register(JavaPlugin plugin)
    {
      if (!isRegistered(plugin)) {
        plugin.getServer().getPluginManager().registerEvents(INSTANCE, plugin);
        this.plugin = plugin;
      }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 23, 2017
  2. Offline

    mine-care

    Thats where the error is, what is on line 47 of MenuListener?
     
    raunak114 likes this.
  3. Offline

    raunak114

    @mine-care I posted the code above at line 47, I believe its the 1st code snippet( the if statement)
     
    Last edited: Mar 24, 2017
  4. Offline

    raunak114

    Bump, please help me.. I just can't figure this out
     
  5. Offline

    mehboss

    @raunak114
    Print "instance" & see if it returns null.

    Make sure inventory isn't null.

    It should be parameters: createInventory(inventoryHolder, size, name) {
     
    Last edited: Mar 24, 2017
    raunak114 likes this.
  6. Offline

    mine-care

    I suppose that is "parameters" :D Yaay for once i correct someone's spelling! XD

    I pointed out the line to allow you to notice what the problem is yourself, but since we reached this point, lets talk about the code muhahaha }:- )
    So, here you have three different constructors each taking a different parameter. This means that when you create the object you will only initialize ONE of those three variables and only one is used throughout the code. Therefore think of the case where we initialize the object with the constructor accepting Items as parameter and then let it run. It would error because 'plugin' is null (As we haven't initialized it anywhere) but its still used.
    Ontop of that the variables all have default access modifier which isnt a good idea, as it doesnt nessesarily comply with Encapsulation. Turn them into privates and access them through accessors (getter methods).
    As the code stands in your post at the moment, i dont think i can say much other than it is causing me a headacke because there are parts missing, code being spread all over the place and indentation is inexistant. That makes the code really hard to read and ofcourse because only part of it is provided we cant even compile it ourselves in an attempt to read it. Please provide the full class that errors and lets go from there.

    Now allow me to move on into other code issues to consider:

    Follow Java Naming Conventions to improve code readability.

    Here for example it looks as if you access a static inner class called MenuListener inside the classtype of menuLib and then invoke the method getInstance() although im convinced that MenuListener is not a static class but rather a variable being wrongly named. If that is the case, change it please in acordace with the Naming Conventions and follow encapsulation rules.

    Just as a note, make sure you understand why you are suppressing the warnings the compiler is throwing at you, don't do it because the IDE recomends so ;)

    I dont really understand why these methods are nessesary, isn't your plugin the one registering its own listeners? Why would it double register? The issue with these methods is that i see the variable 'plugin' of that class being initialized in the first one whereby if this method is not called or that line isnt reached then 'plugin' points to null according to this line:
    which means that it is ready to cause npe's! :D
    Correct those please and we can have a further look into the code if you dont figure the problem in the mean time ;)
     
    raunak114 and mehboss like this.
  7. Offline

    mehboss

    Heh


    Sent from my iPhone using Tapatalk
     
  8. Offline

    raunak114

    @mine-care , dayum, thanks for writing so much , as for the cgive class,. I Needed the three constructors, further in the code (I deleted some/all of my cgive code as it was throwing a NPI, as for the public void register ()... It is for registering the events of he menu listener class,

    And sorry I called the .getInstance in a static way, I'll change that, and as PeR the private Plugin plugin null; is concerned, do I make that plugin into a constructor of my main class?, As I said, the suppress warnings part, I will use the unused objects soon, and in cgive, I (probably) have to use the inventories in my listener class, although the array list should be private ;)
    Thanks again,
    Raunak114


    OK, so I just print both , the instance and the inventory? Also I made sure the createInventory() is written properly ;)
    Thanks,
    Raunak114


    Also, I'll do the code changes and stuff ASAP, that is, when I get home;)
     
    mehboss likes this.
  9. Offline

    mine-care

    Hehe yeah i sometimes have the bad habbit of writing a lot... I would have written even more if it wasnt ~2AM and i didn't have that drink before ;)

    I see, but why not registering them in the conventional way? Why is there the need to create an entire method that calls another method which checks if the plugin has already registered this listener? Thats what i dont really get. If there is the need to check if it is registered that means that there is a possibility that it will already be registered when trying to register it. If that is the case then mabe we need to look at why it is registering more than once rather than fight it with checks.

    Well i didnt really mind that, because as it is an acceptable technique for Singleton objects. My main concern was naming conventions making it look like you where accessing variables in a static way when you where actually simply accessing a variable in another object :p

    I'm not sure which class you are refering to, but as a rule of thumb, yes you need to somehow ensure that this variable will have an assigned value other than null before you use it in the code. In this case passing the object through the constructor is what you need, however i can't generalise this statement to apply to all objects in all cases :p
     
  10. Offline

    raunak114

    Hah 2 am is a daily for me. xD

    So, I just register the events like you usually register them, and ill remove the isregistered() and register(), then I need your help, coz most of my other classes use the methods (in my menuLib package),

    Cool, yeah that's why I registered them as static coz I thought it would not matter to much xD

    My main class is the class that extends javaplugin, (on enable and other stuff..), so I just make a check if that plugin is null or not, and then do it? Like this?
    Code:
    if(!(this.plugin == null))
    {
    //do something
    }
    else
    {
    Logger.log.severe etc.....
    }
    Maybe that my bad habit ... Checking stuff too much xD

    So, I'll just make a constructor of the "Plugin" :D

    BTW, I am back home, so I will be able to message quicker for another 2/2.5 hours :D

    EDIT:
    @mine-care I JUST FREAKING LOVE YOU <3 (no homo thou :p) thanks for telling me how to solve the problem :D <3 <3 Stay in touch please :D

    @mehboss Thanks for telling me how to see my problem was, without you two .. My life would have been ruined xD.

    In case anyone wants to know what the problem was, it was the my plugin was null, How i solved it,
    Added a constructor for my main class (duh..) , and removed the register() and isregistered() methods from my 7-8 classes, and it worked out surprisingly, Marking this as solved :D
     
    Last edited: Mar 25, 2017
Thread Status:
Not open for further replies.

Share This Page