NullPointerException on getContents

Discussion in 'Plugin Development' started by Julian1Reinhardt, Sep 13, 2013.

Thread Status:
Not open for further replies.
  1. I know that it throws a NPE because some stacks are air -> null. But I have no clue how to change that. Can anyone help me with that?
     
  2. Offline

    beastman3226

    Code:java
    1. if(player != null) { //maing sure the player is online
    2. ItemStack[] contents = player.getContents();
    3. ItemStack[] airlessContents;
    4. ArrayList<ItemStack> isList = new ArrayList<>();
    5. for(ItemStack is : contents) {
    6. if(is == null || is.getMaterial() == Material.AIR) {
    7. continue;
    8. } else {
    9. isList.add(is);
    10. }
    11. }
    12. airlessContents = isList.toArray(new ItemStack[]);
    13. }
     
  3. beastman3226
    Errors i got:
    Line 6: The method getMaterial() is undefined for the type ItemStack
    Line 12: Variable must provide either dimension expressions or an array initializer
     
  4. Offline

    beastman3226

    You should be able to debug that yourself. But...
    Try is.getType(). And do (new ItemStack[]{}) instead
     
  5. Still throws a nullpoiterException at
    inventory.getContents();
     
  6. Offline

    beastman3226

    Show me your code please.
     
Thread Status:
Not open for further replies.

Share This Page