Solved Help getting API

Discussion in 'Plugin Development' started by drillbitsworkshop, Sep 6, 2021.

  1. I haven't done this in years and started getting back into it by downloading and decompiling an old plugin of mine. I am getting this error
    Code:
    [22:10:41] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to McMoney v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:588) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:575) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.players.PlayerList.a(PlayerList.java:260) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.network.LoginListener.a(LoginListener.java:163) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.network.LoginListener.c(LoginListener.java:150) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.network.LoginListener.tick(LoginListener.java:74) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.network.NetworkManager.a(NetworkManager.java:237) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.network.ServerConnection.c(ServerConnection.java:165) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1267) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:425) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1172) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1014) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:295) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at net.minecraft.server.MinecraftServer$$Lambda$3786/0x00000000669a6068.run(Unknown Source) [craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
            at java.lang.Thread.run(Thread.java:883) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "me.bukkit.drillbitsworkshop.MoneyAPI.hasMoney(java.lang.String)" because "money" is null
            at me.bukkit.drillbitsworkshop.McMoney.onPlayerJoin(McMoney.java:37) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.17.1.jar:3238-Bukkit-9217b52]
    and this is the line of code to get the API which I believe is causing the problem since the API is what seems to be coming up null
    Code:
    public class MoneyAPI {
      private McMoney plugin;
     
      private static MoneyAPI singleton;
     
      public MoneyAPI(McMoney plugin) {
        this.plugin = plugin;
        singleton = this;
      }
     
      public static MoneyAPI getInstance() {
        return singleton;
      }
    It is probably stupid but like I said its been years.
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. @timtower I am trying to call it through the rest of the plugin but it comes back null every time
    Code:
     @EventHandler
      public void onPlayerJoin(PlayerJoinEvent e) {
        MoneyAPI money = MoneyAPI.getInstance();
        if (!money.hasMoney(e.getPlayer().getName())) {
          money.createPlayerMoney(e.getPlayer());
        } else {
          double current = money.getMoney(e.getPlayer());
          double last = money.getMoneyAtLastLogin(e.getPlayer());
          if (current > last) {
            e.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "You recieved " + ChatColor.GREEN + money.getMoneyString(current - last) + ChatColor.LIGHT_PURPLE + " while you were away.");
          } else if (current < last) {
            e.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "You lost " + ChatColor.RED + money.getMoneyString(last - current) + ChatColor.LIGHT_PURPLE + " while you were away.");
          }
        }
      }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @drillbitsworkshop That does not answer my question.
    Where are you calling new MoneyAPI?
     
  5. @timtower I am not sure what you are asking then. I tried doing this because I realized there was nothing actually setting instance to MoneyAPI
    Code:
    private McMoney plugin;
          private static MoneyAPI instance;
         
          public MoneyAPI(McMoney plugin) {
            this.plugin = plugin;
            instance = new MoneyAPI(plugin);
          }
         
          public static MoneyAPI getInstance() {
            return instance;
          }
    but even that isn't working. I know a few years back when I was doing this I always had to change up the code I posted when I used the decompiler but I don't have the slightest clue how I did it and I have no refences to look back on.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @drillbitsworkshop "new MoneyAPI"
    Where do you call that?
    Because you never set the instance anywhere.
     
  7. @timtower if you look at what I posted I tried doing that. probably wrong but I haven't touched this stuff since 2015 and really don't know what I am doing anymore.
     
  8. Offline

    timtower Administrator Administrator Moderator

    @drillbitsworkshop So you never make an instance of the MoneyAPI to set the instance.
    In your onEnable: new MoneyAPI(this)
     
  9. @timtower I feel dumb. as soon as I put that in onEnable and fixed one error. it works perfectly. Thank you very much for the help.
     

Share This Page