Code stops getting run after an error?

Discussion in 'Plugin Development' started by Switchbladed, Nov 14, 2012.

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

    Switchbladed

    Ok, so I have an error, but it doesn't bring up an error in eclipse, and it works fine, but still throws out an error.
    I need to put an event.setCancelled(true) after it, but it won't cancel the event because the code stops running after that error.

    Code:
    if (chests.containsKey(e.getPlayer().getName())){
    chest_blocks = chests.get(e.getPlayer().getName());
    }
    chest_blocks.add(b.getBlock());
    chests.put(e.getPlayer().getName(), chest_blocks);
    ItemStack[] chest_items = chest.getBlockInventory().getContents();
    for (int i = 0;i<chest_items.length;i++)
    {
    e.getPlayer().getInventory().addItem(chest_items[i]);
    }
    chest.getBlockInventory().setContents(null);
    e.getPlayer().sendMessage(ChatColor.BLUE + "You opened the chest!");
    e.setCancelled(true);
    return;
        }
    I can put it inside the for and it will cancel, but it won't give me the items.
    What might I be able to do?
     
  2. Offline

    coldandtired

    Either add some null checks to avoid the error or look into try/catch/finally.
     
  3. Offline

    Malikk

    im not exactly sure what you're doing, but perhaps a try/catch block would help you sort this out.
     
  4. Offline

    Drew1080

    What's the error that it throws then? We might be able to see whats wrong with it.
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    You should not be trying to catch null pointers, esp in this context.
     
  6. Offline

    Switchbladed

    This is the error I get.
    Code:
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:341)
            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.event.CraftEventFactory.callPlayerInteractEven
    t(CraftEventFactory.java:177)
            at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.j
    ava:366)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:650)
            at net.minecraft.server.Packet15Place.handle(SourceFile:58)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:578)
            at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:215)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:495)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:428)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:818)
    Caused by: java.lang.NullPointerException
            at org.bukkit.craftbukkit.inventory.CraftItemStack.<init>(CraftItemStack
    .java:27)
            at org.bukkit.craftbukkit.inventory.CraftInventory.firstPartial(CraftInv
    entory.java:232)
            at org.bukkit.craftbukkit.inventory.CraftInventory.addItem(CraftInventor
    y.java:258)
            at me.Switchbladed.AdventureChest.AdventureChest.onChestOpen(AdventureCh
    est.java:84)
            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:339)
            ... 16 more
    Line 84 is
    Code:
    e.getPlayer().getInventory().addItem(chest_items[i]);
     
  7. Offline

    Malikk

    Since it's a npe, catching it is not what you wanna do.

    Just add in null checks and you should be good to go.
     
  8. Offline

    Switchbladed

    Null checks???
    Sorry, new to java, this is my first plugin. How might I do that?
    if *** != null maybe??
    Edit: Okay, what should I be checking for null? I've tried a few different things and still can't get it working
     
  9. Offline

    CeramicTitan

    Code:
    if(e.getPlayer().getInventory() !=null){
    e.getPlayer().getInventory().addItem(chest_items[i]);
    }else{
    return; //Player inventory is null then return
    }
     
  10. Offline

    Switchbladed

    CeramicTitan

    Is that meant to go inside my for? Or? (Cause it doesn't work in there)
     
  11. Offline

    CeramicTitan

    replace:

    e.getPlayer().getInventory().addItem(chest_items);
    with post above
     
  12. Offline

    Switchbladed

    CeramicTitan

    Tried that but it didn't work. Thanks for your help

    Also I tried
    try-catch-finally and I just got spammed with it, it cancelled the opening, but didn't give me the items.
     
  13. A simple null check i use is a one-liner:
    if(something == null) return;
     
  14. Offline

    CeramicTitan

    did you revert my changes then add the try and catch block?
     
  15. Offline

    Switchbladed

    CeramicTitan
    Yes, Tried with and without your changes.
    I got it to cancel the opening and display "You opened the chest" But it doesn't give the items.
    I did it without finally though, so I think if there is an error on try, it finds the catch and runs finally instead? I'm not really sure completely on it yet.

    Does anyone have another idea how I could fix this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page