Caused by: java.lang.NullPointerException, Cause random?

Discussion in 'Plugin Development' started by DJSanderrr, Jan 12, 2013.

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

    DJSanderrr

    Line 264: Random randnot = new Random();

    Around line 264:
    Code:
           if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    
                    if (p.getItemInHand().getItemMeta().getDisplayName().contains("§lDrugs")){
                        Random randnot = new Random();
                        int randd = randnot.nextInt(20);
                        if (randd <= 15){
                            if (p.getHealth() <= 5){
                                p.sendMessage("§4You took an Overdose!");
    
    Error:
    Code:
    20:07:23 [SEVERE] Could not pass event PlayerInteractEvent to PlayerInfo v0.5
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at org.bukkit.craftbukkit.v1_4_6.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:178)
            at net.minecraft.server.v1_4_6.PlayerInteractManager.interact(PlayerInteractManager.java:376)
            at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:656)
            at net.minecraft.server.v1_4_6.Packet15Place.handle(SourceFile:58)
            at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
            at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
            at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
            at me.DJsZ.info.PlayerInfo.interact(PlayerInfo.java:264)
            at sun.reflect.GeneratedMethodAccessor342.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.java:425)
            ... 16 more
    
     
  2. Offline

    evilmidget38 Retired Staff

    DJSanderrr I'm going to go ahead and guess two things:
    1. The code you're running to get that error has slightly different lines.
    2. The ItemMeta of the item doesn't have a DisplayName, so when you try to use contains on it, it throws a NPE. Make sure you're check if it has a DisplayName first.
    Just guesses, but give it a go.
     
  3. Offline

    DJSanderrr

    thanks i try

    did it, and now this error:

    Code:
    21:21:10 [SEVERE] Could not pass event PlayerInteractEvent to PlayerInfo v0.5
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at org.bukkit.craftbukkit.v1_4_6.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:178)
            at net.minecraft.server.v1_4_6.PlayerInteractManager.interact(PlayerInteractManager.java:376)
            at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:656)
            at net.minecraft.server.v1_4_6.Packet15Place.handle(SourceFile:58)
            at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
            at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
            at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
            at me.DJsZ.info.PlayerInfo.interact(PlayerInfo.java:262)
            at sun.reflect.GeneratedMethodAccessor351.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.java:425)
            ... 16 more
    >
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  4. Offline

    evilmidget38 Retired Staff

    DJSanderrr I'd need to see your code to help you.
     
  5. Offline

    Seadragon91

    Code:
    p.getItemInHand()

    Are you doing a null check here? Because if you have no item on your hand, then a NPE will be thrown.
    If you forget that null check before. Change this line:
    Code:
     if (p.getItemInHand().getItemMeta().getDisplayName().contains("§lDrugs")){

    to:
    Code:
    if (p.getItemInHand()!= null && p.getItemInHand().getItemMeta().getDisplayName().contains("§lDrugs")){
     
  6. Offline

    DJSanderrr

    THANKSSS!!!

    also an method to take one item, from item in hand?

    and maybe play a sound effect :$

    EDIT:
    that only ONE player can hear it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  7. Offline

    Seadragon91

    The player have already a playSound - method, take a look at this. But I am not sure If other players will hear that too.
     
  8. Offline

    evilmidget38 Retired Staff

    I believe World has a play sound for all players to hear it.
     
  9. Offline

    Seadragon91

    Ok my statement is not good, sorry. He want that only 1 player hear it.
     
  10. Offline

    DJSanderrr

    but you ave to enter 2 floats. i belive 1is the range and one is the volume.

    p.playSound(location, sound, float one, float two)
     
Thread Status:
Not open for further replies.

Share This Page