Solved InventoryClickEvent null proplem

Discussion in 'Plugin Development' started by fokolo, Sep 15, 2013.

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

    fokolo

    this is the code:
    Code:java
    1. public void OnInventoryClick(InventoryClickEvent event) {
    2.  
    3. //simple materials
    4. Material[] ids = {Material.BOW,Material.ARROW,Material.IRON_SWORD,Material.ENDER_PEARL,Material.DIAMOND_CHESTPLATE};
    5. int view = InventoryView.OUTSIDE;
    6.  
    7. if (event != null) {
    8. String log = "view: "
    9. + event.getView().getTitle() +" "+ view + " slot num: "
    10. + event.getSlot();
    11. System.out.println(log);
    12. for(Material i : ids){
    13. if(i==event.getCurrentItem().getType())
    14. {
    15. event.setCancelled(true);
    16. System.out.println("canceled event to "+i.toString());
    17. break;
    18. }
    19. }
    20. if(event.getCurrentItem().getType() == Material.STICK){
    21. if(event.getCurrentItem().getEnchantments().isEmpty())
    22. {
    23. System.out.println("no enchantments");
    24. }else{
    25. System.out.println("has enchantments canceling event");
    26. event.setCancelled(true);
    27. }
    28. }
    29. }
    30. }
    31.  



    if i click (while in inventory) anywhere out side the box i get null error
    as you can see i tried checking if the event is null but no help


    thanks


    error:
    2013-09-15 16:09:23 [SEVERE] Could not pass event InventoryClickEvent to LockItem v0.0.1
    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 net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:1372)
    at net.minecraft.server.v1_6_R2.Packet102WindowClick.handle(SourceFile:31)
    at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
    at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
    at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
    at io.github.fokolo.lockitem.logger(lockitem.java:23)
    at io.github.fokolo.inventoryclicks.OnInventoryClick(inventoryclicks.java:15)
    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.java:425)
    ... 14 more

    half solved i edited the post but i still need help with the null problem

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

    Technius

    fokolo
    1. What is on line 23?
    2. What is your debug message output?
    3. event.getCurrentItem() returns null when you don't click a slot in the inventory.
     
    fokolo likes this.
  3. Offline

    fokolo

    Technius
    1.im checking if the stick is enchanted
    2. debug states if the stick enchanted or not
    3. thank you! you fixed this! :D
     
  4. Offline

    Appljuze

    I've had this same problem...
    A null check won't be enough, because the item in the slot is technically air.

    So how I fixed it was just to add an if statement before all of your listener code
    Code:java
    1. if(event.getCurrentItem() != null && event.getCurrentItem().getType() != Material.AIR)
     
    AJR200 and fokolo like this.
  5. Offline

    Technius

    Appljuze
    It's fine for his purpose since he is checking material types anyways.
     
    fokolo likes this.
  6. Offline

    Appljuze

    I guess it depends on where he puts the if statement
     
    fokolo likes this.
  7. Offline

    fokolo

    thank you both i dont need it to check air too so its fine for my purpose :D

    but thanks
    SOLVED
     
Thread Status:
Not open for further replies.

Share This Page