Solved [Help] I hate these... Null Pointer Exception

Discussion in 'Plugin Development' started by XvBaseballkidvX, Nov 24, 2013.

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

    XvBaseballkidvX

    Hello everyone!
    I am currently working on a Zombie Survive plugin and have run into an issue with my Survival Crates!

    Error:
    Code:
    Caused by: java.lang.NullPointerException
        at me.XvPROTECTEDvX.SurvivalCrate.SurvivalCrate.onSurvivalCrate(SurvivalCrate.java:29)
    Code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onSurvivalCrate(PlayerInteractEvent event){
    4. Player player = event.getPlayer();
    5. Block block = event.getClickedBlock();
    6. Material blockType = block.getType(); //Line where the error is
    7. Inventory inv = Bukkit.createInventory(player, 36, "Survival Crate");
    8. Random ran = new Random();
    9. int i = ran.nextInt(1);
    10. i +=1;
    11. if(blockType == Material.PISTON_BASE){
    12. if(i == 1){
    13. inv.setItem(20, new ItemStack(Material.BREAD));
    14. inv.setItem(9, new ItemStack(Material.IRON_INGOT, 2));
    15. inv.setItem(1, new ItemStack(Material.APPLE));
    16. player.openInventory(inv);
    17. }
    18. //I have more randoms but I didnt want to create a huge Code entry
    19. //I have a cooldown method but that isn't apart of the problem


    Gotta love those Null Pointer Exceptions! <3
    It happens when I interact with a mob. (Left/Right click them)

    Thank you for reading!
    All help is much appreciated!

    EDIT: The Exception also occurs when I Right/Left click air.
     
  2. Offline

    mattrick

  3. Offline

    L33m4n123

    You rightclick a entity and not a solid block. But you try to get the block that was clicked. since you did not clicked a block you get a NPE

    probably you would need the PlayerInteractEntityEvent
     
  4. Offline

    The_Doctor_123

    Because you may not be clicking/interacting with any block. Do a proper check.
     
  5. Offline

    XvBaseballkidvX

    I did:
    Code:
    if(blockType == null) return;
    
    And I still got the error :/ any other suggestions?
     
  6. Offline

    L33m4n123

     
  7. Offline

    XvBaseballkidvX

    L33m4n123 I need see what Block the Player clicked though, or do you think I should add the PlayerInteractEntityEvent with the PlayerInteractEvent?
     
  8. Offline

    L33m4n123

    Oh Wait. So you mean with


    that it works with the crates but gives you NPE's if you click an entity or what?
     
  9. Offline

    1Rogue

    Where did you put that check in your code? Where is your updated code?
     
  10. Offline

    Skye

    You want to check if the block is null, not the type. Besides, if it's a block, it has a type.
     
    XvBaseballkidvX likes this.
  11. Offline

    XvBaseballkidvX

    L33m4n123
    It gives me an NPE when I Right/Left Click something other than a Block. The Crates work fine though (They Open up when I click them).

    Skye thank you, trying that right now!

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

    L33m4n123


    Ohh ok. Yea my bad. I missunderstood you then there.. thought somehow you only want to interact with mobs.. d'oh
     
  13. Offline

    XvBaseballkidvX

    Skye Thank you! That fixed my problem!
     
Thread Status:
Not open for further replies.

Share This Page