NullPointerException on Bukkit.getItemFactory()

Discussion in 'Plugin Development' started by SaxSalute, Oct 26, 2014.

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

    SaxSalute

    I'm working on writing a serialization method for items when I ran into a problem. This segment of code:

    Code:java
    1. public static String serialize(ItemStack stack)
    2. {
    3. String serial = stack.getType().name() + "|" +
    4. stack.getAmount() + "|" +
    5. stack.getDurability() + "|";
    6.  
    7. if (!stack.hasItemMeta())
    8. serial = serial + "||";
    9. else
    10. {
    11. serial = serial + stack.getItemMeta().getDisplayName() + "|";
    12.  
    13. for (String s : stack.getItemMeta().getLore())
    14. serial = serial + s + "\\";
    15.  
    16. serial = serial.substring(0, serial.length() - 1);
    17.  
    18. serial = serial + "|";
    19. }
    20.  
    21. return serial;
    22. }


    is throwing a NullPointerException on the if statement. That made no sense to me since stack is clearly not null as it is referenced just beforehand and not modified. The exception propagates back to the hasItemMeta method in the ItemStack class, which is a one line method that checks the ItemFactory.

    Code:java
    1. public boolean hasItemMeta() {
    2. return !Bukkit.getItemFactory().equals(meta, null);
    3. }


    This just made it more confusing, because that means that ItemFactory has to be null. Some more digging and I found that simply calling Bukkit.getItemFactory() throws a NullPointerException. Does anybody have a clue why this could be?

    So apparently I can't run JUnit tests on Bukkit plugins because Bukkit.anything is null in that situation. That's unfortunate.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  2. Offline

    mythbusterma

    SaxSalute

    Is this issue resolved then? Because it looks like your issue was caused by trying to JUnit test.
     
  3. Offline

    SaxSalute

    It is. I thought I would at least post what I figured out in case any other poor soul tried to do some unit testing.
     
Thread Status:
Not open for further replies.

Share This Page