Sell Sign help

Discussion in 'Plugin Development' started by michael566, Oct 25, 2014.

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

    michael566

    Hi, so I have this code:

    Code:java
    1. @EventHandler
    2. public void onSignSell(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    5. if (e.getClickedBlock().getState() instanceof Sign) {
    6. Sign s = (Sign) e.getClickedBlock().getState();
    7. if (s.getLine(0).equalsIgnoreCase("[SELL]")) {
    8. Material m = Material.STONE;
    9. int total = 0;
    10. ItemStack[] contents = p.getInventory().getContents();
    11. if (contents != null) {
    12. for (int i = 0; i < contents.length; i ++) {
    13. if (contents[I].getType() == m) {[/I]
    14. [I] total += contents[I].getAmount();[/I][/I]
    15. [I] EconomyResponse b = Main.econ.depositPlayer(p.getName(), total * 78);[/I]
    16. [I] p.getInventory().remove(Material.STONE);[/I]
    17. [I] p.updateInventory();[/I]
    18. [I] DecimalFormat format = new DecimalFormat("#,###");[/I]
    19. [I] p.sendMessage(ChatColor.GREEN + "Sold " + contents[I].getAmount() + " Stone for " + "$" +format.format(total * 78));[/I][/I]
    20. [I] }[/I]
    21. [I] }[/I]
    22. [I] }[/I]
    23. [I] }[/I]
    24.  
    25. [I] }[/I]
    26.  
    27. [I] }[/I]


    It sells all the players stone in their inventory all at once.
    But when I click the sign I get a NPE at line 31 which is: if(contents.getType == m) . The sell still goes through but it only sells 64 stone but not all the stone in my inventory.
    I tried putting a else statement after it which just returned, but it didn't work.

    Any suggestions?
    Thanks.

    Anyone?

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

    CraftCreeper6

  3. Offline

    es359

  4. Offline

    michael566

    es359
    Code:
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInteractManager.java:374) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at me.michael566.main.SellSign.onSignSell(SellSign.java:31) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_21]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_21]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_21]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_21]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 15 more
    [08:52:19] [Server thread/INFO]: michael566 lost connection: Disconnected
     
  5. Offline

    michael566

    Can anyone else help me?
     
  6. Offline

    mythbusterma

    michael566

    Because some of the slots in your inventory are empty and contents will be null at that location. Look at the javadocs for inventory again.
     
Thread Status:
Not open for further replies.

Share This Page