Solved ItemMeta causing NPE...

Discussion in 'Plugin Development' started by ZeusAllMighty11, Sep 21, 2013.

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

    ZeusAllMighty11

    It seems the the past few builds I've been using to develop with (all latest dev builds), contain an NPE using ItemMeta..

    Perhaps it could be that i'm using colored names with the § symbol (section), as opposed to using another method..

    Here's an example of code causing a questioning NPE
    Code:
    ItemStack is = new ItemStack(Material.DIAMOND_SWORD, 1);
    ItemMeta meta = is.getItemMeta();
    meta.setDisplayName("§5 I am purple ");
    is.setItemMeta(meta);
    
    NPE will be thrown, saying error on the 3rd line (set display name).. item stack isn't null, however the meta some how is..


    Is this changed?
     
  2. Offline

    metalhedd

    getItemMeta() will return null if there is no ItemMeta. you need to create a new ItemMeta instance w/ getServer().getItemFactory().getItemMeta(Material);

    I stand corrected.
     
  3. Offline

    1Rogue

    Well, it would be null because .getItemMeta(); is returning null. I haven't worked much with ItemMeta personally, but could you just check if that returned null, and if it did initialize it with a new value?

    Could it not have ItemMeta because it's merely a new ItemStack in the first place?

    Try creating a new instance of meta and setting that, instead of grabbing from the ItemStack.
     
  4. Offline

    Janmm14

    1Rogue
    Never do new ItemMeta(), because that gives also an error.
     
  5. Offline

    ZeusAllMighty11

    I haven't made a plugin in so long! I didn't even know there was an item factory you had to use... Thanks!
     
  6. Offline

    metalhedd

    I dont think you need it for anything other than creating new ItemMeta instances to be honest. I've never found another use for it.
     
  7. Offline

    1Rogue


    You cannot create direct instances of interfaces anyhow, you would need to go through ItemFactory.
     
  8. Offline

    ZeusAllMighty11

    Just wondering if anyone knows when this changed?

    I used to have no issues at all using the method above..

    And still getting an NPE when using .getItemMeta() in ItemFactory

    Edit: It only appears when setting lore+display name..
     
  9. Offline

    Jogy34


    That isn't true. If the itemstack's ItemMeta is null it attempts to create a new one when you call the getItemMeta() on that ItemStack: HERE. The only time it is null is if the chosen material is air: HERE. I don't know why in this instance that would be the case but I'm guessing it's probably because that isn't the actual code that is being used.
     
    TheGreenGamerHD likes this.
  10. Offline

    ZeusAllMighty11



    Current code being used exactly:

    Code:java
    1.  
    2.  
    3. public void createMenus()
    4. {
    5. //!f
    6. shopMenu = new IconMenu("§5Shop", 9 * 4, new IconMenu.OptionClickEventHandler()
    7. {
    8. @Override
    9. public void onOptionClick(OptionClickEvent event)
    10. {
    11. switch (event.getPosition())
    12. {
    13.  
    14. }
    15. event.setWillClose(true);
    16. }
    17. }, this)
    18. .setOption(0, configData.getShopItem(Upgrade.C4), "§cC4", "test")
    19. .setOption(1, configData.getShopItem(Upgrade.GRENADE_FRAG), "§cFrag Grenade", "test")
    20. .setOption(2, configData.getShopItem(Upgrade.GRENADE_CLUSTER), "§cCluster Grenade", "test")
    21. .setOption(3, configData.getShopItem(Upgrade.LANDMINE), "§cLandmine", "test")
    22. .setOption(4, configData.getShopItem(Upgrade.HARRIERS), "§cHarrier Strike", "test")
    23. .setOption(5, configData.getShopItem(Upgrade.GRENADE_FLASHBANG), "§cFlashbang", "test")
    24. .setOption(6, configData.getShopItem(Upgrade.RIOTSHIELD), "§cRiot Shield", "test")
    25. .setOption(7, configData.getShopItem(Upgrade.ROCKETLAUNCHER), "§cRocket Launcher (1)", "test")
    26. ;
    27. //f
    28. }
    29.  


    And nisovin's Iconmenu class.. in particular, this method
    Code:java
    1.  
    2.  
    3. private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore)
    4. {
    5. ItemStack is = item.clone();
    6. ItemMeta im = item.getItemMeta();
    7. im.setDisplayName(name);
    8. im.setLore(Arrays.asList(lore));
    9. item.setItemMeta(im);
    10. return is;
    11. }
    12.  
    13. Not sure if it will make sense, but restarting my IDE seemed to fix the issue..
    14.  
    15. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  11. Offline

    metalhedd


    Thanks for correcting me. I could have swore I've had getItemMeta return null on me many times, but I must be mistaken.
     
  12. Offline

    KingNyuels

    TheGreenGamerHD
    Turn this:
    Code:java
    1. private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore)
    2. {
    3. ItemStack is = item.clone();
    4. ItemMeta im = item.getItemMeta();
    5. im.setDisplayName(name);
    6. im.setLore(Arrays.asList(lore));
    7. item.setItemMeta(im);
    8. return is;
    9. }

    To this:
    Code:java
    1. private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore)
    2. {
    3. ItemStack is = item;
    4. ItemMeta im = item.getItemMeta();
    5. im.setDisplayName(name);
    6. im.setLore(Arrays.asList(lore));
    7. is.setItemMeta(im);
    8. return is;
    9. }


    You made a little mistake:
    "item.setItemMeta(im);" - But you want to return is ;)

    KingNyuels
     
  13. Offline

    Jogy34

    In that method, if you send in an itemstack of air it would throw that error. It looks like you are getting all of your item ids from a config file though, so if you are attempting to retrieve an integer from a non-existant (maybe spelled wrong) path I believe it will return 0 which might be the problem. It's also possible that it's set to 0 in the config file. If that isn't the problem then check to make sure that the 'configData.getShopItem(Upgrade.****)' doesn't return 0 for any of the values.
     
Thread Status:
Not open for further replies.

Share This Page