NullPointerException PingAPI

Discussion in 'Plugin Help/Development/Requests' started by martijnpold, Jul 31, 2015.

Thread Status:
Not open for further replies.
  1. I am trying to put some custom text on the player counter on the client multiplayer list (this is possible using PingAPI) but im getting a nullPointerException.

    My code:

    package mpolder;

    import org.bukkit.plugin.java.JavaPlugin;
    import skionz.pingapi.PingAPI;
    import skionz.pingapi.PingEvent;
    import skionz.pingapi.PingListener;


    public class fakePlayer
    extends JavaPlugin
    implements PingListener {

    @@Override
    public void onEnable() {
    PingAPI.registerListener(new PingListener() {
    public void onPing(PingEvent event) {
    event.getReply().setOnlinePlayers(5000);
    }
    });

    }


    @@Override
    public void onPing(PingEvent event) {
    event.getReply().setOnlinePlayers(5000);
    }
    }


    Line of error:

    public static void registerListener(PingListener listener) {
    /* 26 */ listeners.add(listener);
    }



    Error log:

    java.lang.NullPointerException
    at skionz.pingapi.PingAPI.registerListener(PingAPI.java:26) ~[?:?]
    at mpolder.fakePlayer.onEnable(fakePlayer.java:19) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot_server.jar:git-Spigot-1544]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:866) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) [spigot_server.jar:git-Spigot-1544]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(CraftServer.java:753) [spigot_server.jar:git-Spigot-1544]
    at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:326) [spigot_server.jar:git-Spigot-1544]
    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:290) [spigot_server.jar:git-Spigot-1544]
    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot_server.jar:git-Spigot-1544]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot_server.jar:git-Spigot-1544]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot_server.jar:git-Spigot-1544]
     
  2. Moved to Bukkit Alternates.
     
  3. Offline

    Boomer

    Null at:
    listeners.add(listener);

    If "listener" is null, that will have no impact on adding it to a collection of any sort.
    .add is a method and will never have a problem being null, but, if the object itself is null, then the method wont exist, thus
    listeners is null at the point in execution
     
  4. This is the code used where the listener gets added, is it wrong?:
    Listener cant be null because in my code i just made a new PingListener so thats not possible. The only thing i can think of is an error with the Listeners array
    Code (open)
    Code:
     package skionz.pingapi;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    public class PingAPI extends org.bukkit.plugin.java.JavaPlugin
    {
    private static List<PingListener> listeners;
    public void onEnable()
    {
    try{
    listeners = new java.util.ArrayList();
    String name = Bukkit.getServer().getClass().getPackage().getName();
    String version = name.substring(name.lastIndexOf('.') + 1);
    Class<?> injector = Class.forName("skionz.skionz.pingapi." + version + ".PingInjector");
    Bukkit.getPluginManager().registerEvents((org.bukkit.event.Listener)injector.newInstance(), this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void registerListener(PingListener listener) {
    listeners.add(listener);
    }
    public static List<PingListener> getListeners() {
    return listeners;
    }
    }
     
    Last edited: Jul 31, 2015
  5. Offline

    Boomer

    You can't have multiple classes extending JavaPlugin, just the main one. And only the main one has an onEnable()
    Throw some debugs in and you'll confirm that you dont actually execute a bunch of code that yout hink are being executed
     
  6. How would i fix this issue? I currently just imported the modules and added them to my project (i use intelliJ) and went from there. How do i make them seperate and still work together? With other words, how do i efficiently import the API
     
  7. Offline

    Boomer

    The only real answer is "by doing it right" which has many different solutions, and can't really be explained.

    An api simply exposes objects and functions for you to impliment, so.. you would create objects and functions to impliment them.

    If you are really using a "PingAPI" then you dont have a PingAPI class in your project...
    You make reference to a pingAPI object from that , you do not import its raw code. The whole point of an api is "You need not worry about what Im doing in this class/classes, just that you need to access me through these functions"
     
  8. How do i import the api then? if i cannot have multiple onEnables and JavaPlugins i must be doing something wrong...
     
  9. Offline

    Boomer

    You are donig somethign wrong.
    You need the api jar in your build path only. Do not import its code.

    You then have only your plugin code, your plugin classes.

    If it is a real api then there will be instructions on how to access and initialize something
    Otherwise, you would just access functions in it the same as you do when you want to access the Bukkit.getServer().getPluginManager() etc --- Bukkit is an API
     
Thread Status:
Not open for further replies.

Share This Page