PotionType null?

Discussion in 'Plugin Development' started by JustinsCool15, Aug 19, 2014.

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

    JustinsCool15

    So this plugin I'm making is to disable certain potion types from being brewed.

    The debug messages I'm getting are:
    D1
    D3-6

    I'm getting an NPE on line 24 of the following code.

    Code:java
    1. @EventHandler
    2. public void onBrew(BrewEvent e){
    3. for(ItemStack item : e.getContents()){
    4. if(item != null && item.getType() == Material.POTION){
    5. try{
    6. pot = Potion.fromItemStack(item);
    7. Bukkit.broadcastMessage("D1");
    8. }catch(Exception ex){
    9. Bukkit.broadcastMessage("D2");
    10. return;
    11. }
    12.  
    13. Bukkit.broadcastMessage(pot.toString() + " : " + pot.getLevel());
    14. Bukkit.broadcastMessage("D3");
    15. if(pot != null){
    16. Bukkit.broadcastMessage("D4");
    17. if(!(potions.isEmpty())){
    18. Bukkit.broadcastMessage("D5");
    19. for(String str : potions){
    20. Bukkit.broadcastMessage("D6");
    21. String[] words = str.split(":");
    22. Integer lvl = Integer.parseInt(words[1]) - 1;
    23.  
    24. if(pot.getType().toString() != null && pot.getType().toString().equals(words[0])){
    25. Bukkit.broadcastMessage("D7");
    26.  
    27. if(pot.getLevel() != 0 && pot.getLevel() == lvl){
    28.  
    29. Bukkit.broadcastMessage("D8");
    30. e.setCancelled(true);
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }
    37. }
    38. }
     
  2. Offline

    SpaceManiac

    In "pot.getType().toString() != null && pot.getType().toString().equals(words[0])", the first condition is always false (at least in a sane environment, toString() will never return null). Either "pot" or "pot.getType()" might be null for an NPE to happen on that line. You already check "pot" for null, so check "pot.getType()" too.
     
  3. Offline

    JustinsCool15

    SpaceManiac
    pot.getType() is returning null whenever I brew a Strength pot... So the type shouldn't be null.
     
Thread Status:
Not open for further replies.

Share This Page