Solved Null pointer execption problem

Discussion in 'Plugin Development' started by maestro34, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. How to avoid a NullPointerExeception on this code ?
    Code:java
    1. private static boolean isBlue(ItemStack i)
    2. {
    3. if(i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BLUE + "" + ChatColor.BOLD + "Rejoint l'équipe Bleue")){
    4. return true;
    5. }else{
    6. return false;
    7. }
    8. }


    Error:
    Code:
    [20:07:09] [Server thread/ERROR]: Could not pass event InventoryClickEvent to TheTowers v1.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.PlayerConnection.a(PlayerConnection.java:1471) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.PacketPlayInWindowClick.a(SourceFile:32) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.PacketPlayInWindowClick.handle(SourceFile:10) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.NetworkManager.a(NetworkManager.java:148) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.ServerConnection.c(SourceFile:134) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:654) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:250) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:545) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:458) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
    Caused by: java.lang.NullPointerException
        at me.maestro34.tower.Events.TeamPickerEvents.isBlue(TeamPickerEvents.java:199) ~[?:?]
        at me.maestro34.tower.Events.TeamPickerEvents.onInventoryInteract(TeamPickerEvents.java:89) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[sportbukkit.jar:git-SportBukkit-1.7.2-R0.3-78-g57464eb]
        ... 13 more
     
  2. Offline

    Wizehh

    maestro34
    Check if the itemstack's display name and item meta are null first:
    PHP:
    if (i.getItemMeta() != null) {
        if (
    i.getItemMeta().getDisplayName() != null) {
            
    // check if display name equals
        
    }
    }
     
  3. Offline

    1Rogue

    Use "hasItemMeta" rather than checking for null:

    Code:java
    1. String comp = ChatColor.BLUE + "" + ChatColor.BOLD + "Rejoint l'équipe Bleue";
    2. if (i.hasItemMeta()) {
    3. if (comp.equalsIgnoreCase(i.getItemMeta().getDisplayName()) {
    4. //has item meta, and is equal display names
    5. }
    6. }
     
  4. Wizehh Thanks it's working :)
     
  5. Offline

    Wizehh

    What's the difference?
     

  6. I used your way :) Thanks
     
  7. Offline

    1Rogue


    Two less null checks, conformity to API, and you don't have to check if the display name is null.
     
  8. Offline

    Wizehh

    Oh, okay
     
Thread Status:
Not open for further replies.

Share This Page