Solved Error in event class. (NullPointExpectation)

Discussion in 'Plugin Development' started by dznSpencer, Jul 26, 2015.

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

    dznSpencer

    Using vault to reward money with a scratching ticket like you'd get at a gas station.

    Whenever I right click this happens. I'm stuck on it.

    Class
    ----------
    Code:
    package me.dznSpencer.SimpleGambling.events;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    public class ScratchTicket implements Listener {
    
        public static Economy econ = null;
    
        @EventHandler
        public void onRightClickEvent(PlayerInteractEvent e) {
    
            String prefix = "§a[§fGambling§a] §f";
    
            Player p = e.getPlayer();
            String pn = p.getName();
    
            ItemStack Scratch = new ItemStack(Material.PAPER);
            ItemMeta SM = Scratch.getItemMeta();
            SM.setDisplayName("§a§lScratch §f§lTicket");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add("§Test your luck!");
            SM.setLore(lore);
            Scratch.setItemMeta(SM);
    
            Random money = new Random();
            int gene = money.nextInt(1500);
    
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (p.getItemInHand().isSimilar(Scratch)) {
                     EconomyResponse r = econ.depositPlayer(p, gene);
                    if (r.transactionSuccess()) {
                        Bukkit.broadcastMessage(prefix + ChatColor.GREEN + ChatColor.ITALIC + p.getName() + "§f got §a"
                                + gene + "$ §ffrom a Scratch Ticket!");
                        }
                    }
                }
            }
        }
    
    Error (In Console)
    ---------------------------

    Code:
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:227) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:194) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:190) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :717) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_51]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    14) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    53) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :556) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
            at me.dznSpencer.SimpleGambling.events.ScratchTicket.onRightClickEvent(S
    cratchTicket.java:45) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
            ... 18 more
    >
     
  2. Offline

    ninjaboy323

    @dznSpencer when you are declaring your "Economy econ" you are setting it to null that is what is causing the issue. No need for the "= null" part, just this: "public static Economy econ;"
     
  3. Offline

    dznSpencer

    Changed that just now, Didn't fix a thing.
     
  4. Offline

    ninjaboy323

    @dznSpencer did you add Vault to your server's plugins folder and depend or soft depend it in your plugin.yml?

    Also make sure that this is in your class that you setup the economy in:

    Code:
    private boolean setupEconomy()
        {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
    
            return (economy != null);
        }
    and also make sure that this method is being called onEnable
     
  5. Offline

    dznSpencer

    I don't think you can call it on enable. It's has a private in t. Should I just remove it?

    I have vault loaded in my plugins aswell as my libraries.
     
  6. Offline

    MCMatters

    @dznSpencer omg, learn java, private means its only accessible in that class only, and if you read vault api, you'd know that you can call it in onEnable and you are supposed to.

    @dznSpencer
    That is un nessicary to change.

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

    dznSpencer

    Main class for adding vault is working fine. It's this class.

    Line 45 N.P.E (NullPoint)


    This is the class. (Of scratch ticket)

    Code:
    package me.dznSpencer.SimpleGambling.events;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    public class ScratchTicket implements Listener {
    
        public static Economy econ = null;
    
        @EventHandler
        public void onRightClickEvent(PlayerInteractEvent e) {
    
            String prefix = "§a[§fGambling§a] §f";
    
            Player p = e.getPlayer();
            String pn = p.getName();
    
            ItemStack Scratch = new ItemStack(Material.PAPER);
            ItemMeta SM = Scratch.getItemMeta();
            SM.setDisplayName("§a§lScratch §f§lTicket");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add("§fTest your luck!");
            SM.setLore(lore);
            Scratch.setItemMeta(SM);
    
            Random money = new Random();
            int gene = money.nextInt(1500);
    
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (p.getItemInHand().isSimilar(Scratch)) {
                     EconomyResponse r = econ.depositPlayer(p, gene);
                    if (r.transactionSuccess()) {
                        Bukkit.broadcastMessage(prefix + ChatColor.GREEN + ChatColor.ITALIC + p.getName() + "§f got §a"
                                + gene + "$ §ffrom a Scratch Ticket!");
                        }
                    }
                }
            }
        }
    
     
  8. Offline

    guitargun

    Where did you set the value?? I still see it as null.. Try using a constructor for it otherwise
     
  9. Offline

    dznSpencer

    Value of?...
     
  10. Offline

    MCMatters

  11. Offline

    SuperOriginal

  12. Offline

    guitargun

  13. Offline

    dznSpencer

    Econ is from an import.



    Code:
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    public class ScratchTicket implements Listener {
    
        public static Economy econ = null;
    
    
    
    I just don't understand. What could be a nullpointexeption here?

    Code:
                     EconomyResponse r = econ.depositPlayer(e.getPlayer(), gene);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  14. Offline

    SuperOriginal

    econ is null. Instead of ignoring my links, you should probably learn from them.
     
  15. Offline

    ninjaboy323

    @dznSpencer it is very hard to help someone who has minimal knowledge of Java. Use the links and learn a little more before attempting to hook into an API that is somewhat complicated like Vault.
     
Thread Status:
Not open for further replies.

Share This Page