Solved What is wrong?

Discussion in 'Plugin Development' started by Iervolino, Jun 24, 2013.

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

    Iervolino

    Code:

    Code:
    @EventHandler
    public void PlayerInteract(PlayerInteractEvent event){
        Location location = event.getClickedBlock().getLocation();
        Block block = event.getClickedBlock();
        if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
            if(block.getType() == Material.TRAPPED_CHEST){
                block.getWorld().createExplosion(location, 3.0F);
            }
        }
    }
    When I open the Trapped Chest, it explodes and I get this crash in the console:

    Code:
    12:46:24 [SEVERE] Could not pass event PlayerInteractEvent to IervoAbilities v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:186)
            at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:156)
            at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java
    :1004)
            at net.minecraft.server.v1_5_R3.Packet18ArmAnimation.handle(SourceFile:4
    1)
            at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292
    )
            at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java
    :109)
            at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:5
    81)
            at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
    77)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :410)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.NullPointerException
            at me.Iervolino.IervoAbilities.IervoAbilities.PlayerInteract(IervoAbilit
    ies.java:43)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 16 more
    What is wrong with the code?
     
  2. Offline

    Rocoty

    Line 43?
     
  3. Offline

    Iervolino


    Code:
    Location location = event.getClickedBlock().getLocation();
     
  4. Offline

    Wingzzz

    Key point:
    Code:
    Caused by: java.lang.NullPointerException at me.Iervolino.IervoAbilities.IervoAbilities.PlayerInteract(IervoAbilities.java:43)
    You're receiving a Null Pointer Exception(what's that?) at IervoAbilities.java on line 43.

    If you're unable to figure out what's wrong, please share that class with accurate line count via using:
    Code:
    [syntax=java]// code here[/syntax]
    in reply to this thread.

    It would help us, help you :) Also try to keep it formatted as best as possible, really makes things all that much easier!
     
  5. Offline

    Iervolino

    Line 43:

    Code:java
    1. Location location = event.getClickedBlock().getLocation();
     
  6. Offline

    Wingzzz

    Iervolino
    Are you checking to see if what is getting clicked is a block before you're doing that? If it's not a block it could possibly be null which is why it would be spewing out such an error.
     
  7. Offline

    Rocoty

    I think he meant share the entire class
     
  8. Offline

    Iervolino


    No, it's is a block.. Look the code.

    Code:java
    1. @EventHandler
    2. public void PlayerInteract(PlayerInteractEvent event){
    3. Location location = event.getClickedBlock().getLocation();
    4. Block block = event.getClickedBlock();
    5. if((event.getAction() == Action.RIGHT_CLICK_BLOCK) && (block.getType() == Material.TRAPPED_CHEST)){
    6. block.getWorld().createExplosion(location, 3.0F);
    7. }
    8. }
     
  9. Offline

    Wingzzz

    Iervolino
    I know it is a block you want, but you're getting the interact event, so you want to first just check to see if it is a block to ensure there is no NPE. Which you create on line 43 when it's not a block. It's the same way we check to see if a command sender is a player, although we know 90% of the time they are, we still do a check so we don't get any NPE when it's not a player and it's a console perhaps.
     
  10. Offline

    Iervolino


    Ok, so how I check it? Sry, I'm noob.
     
  11. Offline

    Ivan

    Put the location and the block after the action checking. That way you'll be checking it without removing or adding code
     
  12. Offline

    Hoolean

    The NPE is caused by the usage of getLocation() on the event.getClickedBlock(), which has not been checked to see if it is null or not.

    EDIT: Ninja'd twice :eek:
     
  13. Offline

    Iervolino


    Yes, I already understood it, but I don't know how to check if is null or not...
     
  14. Offline

    Ivan

    My first post may not have been that clear. What the error is, with the lines:
    Location location = event.getClickedBlock().getLocation();
    Block block = event.getClickedBlock();
    You're assuming that you are clicking a block.
    AFTER these lines you put this line:
    if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    Which is checking if you're actually right clicking a block.
    You will get a lot of NPE's since, every time you left- or right-click the event gets fired.

    Solution:
    Move these lines:
    Location location = event.getClickedBlock().getLocation();
    Block block = event.getClickedBlock();
    after

    if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
     
    Iervolino likes this.
  15. Offline

    Wingzzz

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. Location location = event.getClickedBlock().getLocation();
    5. Block block = event.getClickedBlock();
    6. if(block.getType().equals(Material.TRAPPED_CHEST)) {
    7. block.getWorld().createExplosion(location, 3.0F);
    8. }
    9. }
    10. }

    So basically moving location and block after the check to ensure they aren't null.

    EDIT: Should work, although not tested.
     
    Iervolino likes this.
  16. Offline

    Hoolean

    Iervolino

    You've got to check if event.getClickedBlock() is null before using it for something like getLocation() :p
     
  17. Offline

    Wingzzz

    Right right ^, love how I didn't even add that ahaha
     
  18. Offline

    Iervolino

    Thanks, worked!
     
  19. Offline

    Hoolean

    Iervolino

    Just to add an explanation of why Wingzzz code works:

    If the PlayerInteractEvent has been triggered by a Player hitting the air (no block involved) event.getClickedBlock() returns null and using any methods from it will cause a NullPointerException.

    This is why it's important to check what type of PlayerInteractEvent it is first :)
     
    Iervolino and Wingzzz like this.
Thread Status:
Not open for further replies.

Share This Page