Solved Help me fix this stupid null pointer please!

Discussion in 'Spigot Plugin Development' started by Str8Outa, Mar 26, 2019.

  1. Hey Spigot, so recently I ran into a really confusing NPE. I've been trying for over an hour to see if it was simply a stupid mistake but I've had no luck.

    Here's my code:

    Here's part of the config:

    Here's the stack trace:

    It's throwing an NPE at
    Code (Text):
    int menuSize = plugin.getConfig().getInt("blacksmithmenu.menusize");
    which doesn't make any sense, because that's obviously a path defined in the config.
  2. Have you initialized your "plugin" variable you are using to get the config?
    • Like Like x 1
  3. Code (Java):
    int menuSize = plugin.getConfig().getInt("blacksmithmenu.menusize");
    Thats where the Error comes from^^
  4. Well yeah, I am aware.
    But since I can't see anything wrong with the config path, the only other option I see is that his plugin variable may not have been initialized thus being null
  5. SteelPhoenix


    You use your plugin variable before actually defining it, thus you get a NPE. menuInv gets initialized before the code in the constructor gets run
    • Like Like x 1
  6. Please show where you registered the event
    • Like Like x 1
  7. I defined "plugin" right above my "loadBlacksmithMenu" function though? Here is the main class, the event is registered in the "registerEvents()" function,
  8. As I thought, I just made a stupid simple mistake. Thank you all for your help!
  9. drives_a_ford

    Junior Mod

    Just because you write it "above" doesn't mean it gets called before. You've created an instance variable that runs a method to get its value. That method uses another instance variable which gets defined only within the constructor.
    When you invoke a constructor, the instance variable initializers run before the body of the constructor.