Solved [Problem] Demonic NullPointerException

Discussion in 'Spigot Plugin Development' started by SrLegsini, May 29, 2016.

  1. I have a problem at my plugin, config and the inventories.

    Explanation:
    These are my inventories:
    Code (Text):
        Inventory staffInv = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.MenuName"));
        Inventory optionsMenu = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.OptionsAndStatsMenuName"));
    When I load them, I receive an error in the console.
    Yep, the NullPointerException referring to the "this.getConfig()".

    All config is perfect, and if you want, here's the full code:
    (The inventories are at the line 735 & 736)

    https://github.com/MrLechuga/SimpleStaffControl2/blob/master/MClass.java

    Console:

    [​IMG]

    Help = Potato! :)
     
    • Funny Funny x 1
    • Informative Informative x 1
  2. The config must be null then.
     
  3. It's not. I tested using strings without getting it from config, and it works at this way, but I want that the user can config the menu name.
     
  4. There's a way to change an inventory's name?
    If it's, I can test editing it when a player opens it.
     
  5. 'demonic' I'm dying of laughter
     
  6. Please stop double-posting...
    As for your issue, I guess it's caused by an issue with your config. Either your config path is null or wrong, either your config itself is null.
     
  7. I made tests with commands, and all paths are correct...
     
  8. Ok, so, you say that when declaring your inventories you use a constant string instead of getting it from the config it works?
    Like, this does work:
    Code (Text):
        Inventory staffInv = Bukkit.createInventory(null, 54, "STRING");
    while this doesn't?
    Code (Text):
        Inventory staffInv = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.MenuName"));
     
  9. Yes, it works, but I want that the user can modify the inventory's title.
     
  10. Than the config is null or the path.
     
  11. That's indeed the only possible explanation.
     
    • Agree Agree x 2
  12. I don't think that would make any difference, he said it worked when he was simply specifying a string at initialization.
     
  13. The config has not been initialized yet at the point he is creating the inventory. So my guess is that its setting the title to null and that is causing the exception. Could be wrong but either way that initialization needs to be moved to the onEnable method. I would have to check the source to see if null is allowed for Inventory title but I am almost positive it is not.

    EDIT:
    Yeah can't be null.
    Code (Text):

      public MinecraftInventory(InventoryHolder owner, int size, String title) {
          Validate.notNull(title, "Title cannot be null");
          this.items = new ItemStack[size];
          this.title = title;
          this.viewers = new ArrayList<HumanEntity>();
          this.owner = owner;
          this.type = InventoryType.CHEST;
      }
     
     
    #14 NinjaStix, May 29, 2016
    Last edited: May 29, 2016
    • Agree Agree x 1
  14. Oh, I didn't understand correctly what you meant first. Yes, that might be a good idea.
     
    • Agree Agree x 1
  15. And if I make a delay of 1 second to initialize the inventories... Would this work?
     
  16. Are you initializing them as fields? If you do that before onEnabled is fired you will cause an NPE with that line.
     
    • Agree Agree x 1
  17. Doesn't work that way. Just change it the way I showed in the other thread. Reposted here:
    Move the initialization of the two inventories to inside the onEnable() method

    Take this:
    Code (Text):

    Inventory staffInv = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.MenuName"));
    Inventory optionsMenu = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.OptionsAndStatsMenuName"));
     
    and make it:
    Code (Text):

    Inventory staffInv = null;
    Inventory optionsMenu = null;

    @Override
    public void onEnable() {
        //save config, etc.
        staffInv = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.MenuName"));
        optionsMenu = Bukkit.createInventory(null, 54, this.getConfig().getString("GUI.OptionsAndStatsMenuName"));
    }
     
     
    • Agree Agree x 1
    • Winner Winner x 1
  18. It works!
    Thank you so much! :D
     
    • Friendly Friendly x 2