Solved Return error when does not in for

Discussion in 'Plugin Development' started by mAndAle, Jun 7, 2020.

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

    mAndAle

    my problem is that the return of the boolean shouldn't give an error, yet it does

    Here the code:

    PHP:
    else if (clicked.equals(book1)){
                        
    int bal 10000;
                        
    e.setCancelled(true);
                        
    boolean b false;
                        for (
    ItemStack i p.getInventory().getContents()) {
                            if (
    i.getType() == Material.DIAMOND_PICKAXE) {
                                if (
    TokensEconomy.getTokens(p) >= bal) {
                                    
    TokensEconomy.removeToken(pbal);
                                    
    p.sendMessage(Main.prefix ChatColor.GREEN "You have been adding Detonation!");
                                    
    ItemMeta im i.getItemMeta();
                                    
    ArrayList<Stringlore = new ArrayList<String>();
                                    if (
    im.hasLore()) {
                                        
    lore = (ArrayList<String>) im.getLore();
                                        
    lore.add("Detonation");
                                    } else {
                                        
    lore.add("Detonation");
                                    }
                                    
    im.setLore(lore);
                                    if (
    i.getEnchantments().isEmpty()) {
                                        
    im.addEnchant(Enchantment.ARROW_INFINITE1true);
                                        
    im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                                    }
                                    
    i.setItemMeta(im);
                                    
    true;
                                    
    p.updateInventory();
                                    break;
                                }
                            }
                        }
                        if (!
    bp.sendMessage(Main.prefix ChatColor.RED "You don't have a pickaxe in your inventory");
    Here the error:

    Code:
    Could not pass event InventoryClickEvent to Tokens v1.0
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1891) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at java.util.ArrayList.addAll(Unknown Source) ~[?:1.8.0_241]
        at org.mAndAle.Tokens.Inventories.onClick(Inventories.java:125) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        ... 15 more
    [04:30:37]
    Anyone can help me?
     
    Last edited: Jun 7, 2020
  2. Offline

    caderapee

    @mAndAle show the error. There 's many thing that can be null in your code
     
  3. Offline

    mAndAle

    Code:
    Could not pass event InventoryClickEvent to Tokens v1.0
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1891) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at java.util.ArrayList.addAll(Unknown Source) ~[?:1.8.0_241]
        at org.mAndAle.Tokens.Inventories.onClick(Inventories.java:125) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        ... 15 more
    [04:30:37]
     
  4. Offline

    Strahan

    What is line 125? This is why it's better to post the whole class rather than a snippet.

    Code:
    boolean b = false;
    You should really use proper, descriptive variable names.

    Code:
    if (i.getType() == Material.DIAMOND_PICKAXE) {
    If you want to save some unnecessary indentation, do
    Code:
    if (i.getType() != Material.DIAMOND_PICKAXE) continue;
    ...instead. Negative checks are, IMO, the better way to go.

    Similarly
    Code:
    if (TokensEconomy.getTokens(p) < bal) continue;
    Code:
    p.sendMessage(Main.prefix + ChatColor.GREEN + "You have been adding Detonation!");
    If you intend to distribute this, you should use a config for messaging to retain flexability. Also, that's not proper grammar unless this really is a continuous process.

    Code:
    ArrayList<String> lore = new ArrayList<String>();
    That should really be
    Code:
    List<String> lore = new ArrayList<String>();
    to follow SOLID design principles.

    Code:
    lore = (ArrayList<String>) im.getLore();
    The prior suggestion would also allow you to avoid the need for this casting. In fact, that whole lore section could be optimized to just:
    Code:
    List<String> lore = im.getLore();
    lore.Add("Detonation");
    im.setLore(lore);
    There. Done in three lines what you do in eight.

    Code:
      if (!b) p.sendMessage(Main.prefix + ChatColor.RED + "You don't have a pickaxe in your inventory");
    Ah, now I see what you are doing with b. That is alltogether unnecessary. You can check immediately if a dpick is in their inv. Example:
    Code:
    playerObject.sendMessage("You do" + ((p.getInventory().contains(Material.DIAMOND_PICKAXE))?"":" not") + " have a dpick");
    Just some thoughts.

    PS, if the ternary operator made that look confusing basically you can do a contains on a player's inventory and pass an ItemStack or Material to look for. Easy peasy. You should really read the docs, a lot of good info there.
     
  5. Offline

    mAndAle


    In order to add a lore to a lore that exist already is not how I do?
     
  6. Offline

    Strahan

    Yea, you can do it that way you were, it's just a bit of unnecessary lines.

    Also you didn't answer the "what is line 125" question.
     
  7. Offline

    mAndAle

    I resolved the error so it's fine, thanks.
     
Thread Status:
Not open for further replies.

Share This Page