Solved Weird null pointer...

Discussion in 'Plugin Development' started by mine-care, Dec 23, 2014.

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

    mine-care

    Hello.
    At the moment i have this method:
    Code:
        public static ItemStack setName(ItemStack is, String name) {
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(name);
            is.setItemMeta(im);
            return is;
        }
    The method works but when i create a itemstack with this method:
    Code:
        public ItemStack toItemStack() {
            ItemStack is = new
    ItemStack(Material.getMaterial(id), ammount,data);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(name);
            im.setLore(lore);
            is.setDurability(durability);
            for (int i : enchantments.keySet()) {
                im.addEnchant(Enchantment.getById(i), enchantments.get(i), true);
            }
            is.setItemMeta(im);
            return is;
        }
    When i try with a itemstackcomming from the code above it throws a null pointer at:
    im.setDisplayName(name);

    If i try to print the itemMeta (as a debug thing) i get this:
    UNSPECIFIC_META:{meta-type=UNSPECIFIC, lore=[]}
    What am i killing?
    Thanks
     
  2. Offline

    aaomidi

    Are you sure 'name' isn't null?

    You need to add some null checks and print out debug messages.
     
  3. Offline

    mine-care

    @aaomidi Nothin of those variables is null D:
    i checked them one by one! Also i have if's checking if anything is null, if it is they set a value themselfs like "" or 0
    Thanks for your reply =)

    EDIT:
    to get the name i use this method i made:
    Code:
        public String getNameOfItem(ItemStack is) {
            if(is == null) return "";
            if (is.hasItemMeta()){
                if( is.getItemMeta().hasDisplayName()) {
                return is.getItemMeta().getDisplayName();
                }
            }
            return "";
        }
     
  4. Offline

    MisterErwin

    Are you sure
    returns something valid?
    Maybe print the name of the Material ;)
     
  5. Offline

    mine-care

    @MisterErwin Thanks! your sugestion lead me to debug this part as well, it turns out that id was 0 because it was falling under the itemstack == null if statement :) i just noticed that inventory.getcontents() returns many many nulls... :( i was adding all those null itemstacks as well :/ Thanks! Solved.
     
Thread Status:
Not open for further replies.

Share This Page