Solved NPE which I can't seem to identify.

Discussion in 'Plugin Development' started by TsjipTsjip, Dec 2, 2018.

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


    Hello everyone, I'm developing a plugin and when I register an event, it's been throwing an NPE. Generally, this means that one of the arguments I provided is null. (Please correct if wrong.) I've verified both arguments to registerEvents();, and they are both defined correctly (but apparently that's not the case since it's throwing an NPE. :D )

    I don't know what to do. I'm hoping one of you can help me out on this one.

    Server version: 1.13.2
    Spigot library version: 1.13.2

    Error occurred while enabling NNClasses v0.0.1 (Is it up to date?)
    java.lang.NullPointerException: null
    at org.bukkit.plugin.SimplePluginManager.registerEvents( ~[craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at io.github.tsjiptsjip.classplugin.listeners.InvClickListener.<init>( ~[?:?]
    at io.github.tsjiptsjip.classplugin.Main.onEnable( ~[?:?]
    at ~[craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugin( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugins( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.l( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.DedicatedServer.init( [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at [?:1.8.0_181]
    Relevant classes:
    Main class:
    package io.github.tsjiptsjip.classplugin;
    import io.github.tsjiptsjip.classplugin.UI.ClassesUI;
    import io.github.tsjiptsjip.classplugin.commands.ClassesCommand;
    import io.github.tsjiptsjip.classplugin.listeners.InvClickListener;
    public class Main extends JavaPlugin {
        public void onEnable() {
            new InvClickListener(this); // <------------ Line 14
            new ClassesCommand(this);
    InvClickListener class:
    package io.github.tsjiptsjip.classplugin.listeners;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.plugin.PluginManager;
    import io.github.tsjiptsjip.classplugin.Main;
    import io.github.tsjiptsjip.classplugin.UI.ClassesUI;
    public class InvClickListener implements Listener {
        private Main plugin;
        public InvClickListener(Main plugin) {
            plugin = this.plugin;
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(this, this.plugin); // <-------------- Line 21
        public void onClick(InventoryClickEvent e) {
            String title = e.getInventory().getName();
            if (title.equals(ClassesUI.inv_name)) {
                if (e.getCurrentItem() == null) {
                if (title.equals(ClassesUI.inv_name)) {
                    ClassesUI.clicked((Player) e.getWhoClicked(), e.getSlot(), e.getCurrentItem(), e.getInventory());
    I think that's all. Thanks in advance, hope to hear back soon. (I don't know how to add codeblocks, though all code shown starts from line one. I've marked relevant lines in comments.)

    EDIT: It appears codeblocks do work, but the line numbers still aren't shown. I left the line markers where they were at the time of posting.
    Last edited: Dec 2, 2018
  2. Offline



    To make things easier, I got the codes from SimplePluginManager from

    public void registerEvents(Listener listener, Plugin plugin) {
            if (!plugin.isEnabled()) {
                throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled");
            for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry : plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) {
    But the real problem is not there, it is just in your code.

    See this line?
    plugin = this.plugin;
    I suggest you invert that line
  3. Offline


    Thanks for all of the replies on this (some of you awesome people replied in direct conversations rather then here), my mistake was rather obvious, I needed to switch around plugin = this.plugin;.

    I implemented the change and it’s now throwing another exception somewhere else regarding an illegal material. Not to worry, I think I’ll have it squashed in no time.

    Issue resolved.
Thread Status:
Not open for further replies.

Share This Page