Solved NPE which I can't seem to identify.

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

  1. Offline

    TsjipTsjip

    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

    Stacktrace:
    Code:
    Error occurred while enabling NNClasses v0.0.1 (Is it up to date?)
    java.lang.NullPointerException: null
    at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginManager.java:520) ~[craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at io.github.tsjiptsjip.classplugin.listeners.InvClickListener.<init>(InvClickListener.java:21) ~[?:?]
    at io.github.tsjiptsjip.classplugin.Main.onEnable(Main.java:14) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:254) ~[craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:331) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugin(CraftServer.java:421) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugins(CraftServer.java:347) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.l(MinecraftServer.java:575) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:537) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:415) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.DedicatedServer.init(DedicatedServer.java:270) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:680) [craftbukkit-1.13.2.jar:git-Bukkit-282dad1]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_181]
    Relevant classes:
    Main class:
    Code:
    package io.github.tsjiptsjip.classplugin;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    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 {
    
        @Override
        public void onEnable() {
        
            new InvClickListener(this); // <------------ Line 14
        
            ClassesUI.initialize();
        
            new ClassesCommand(this);
        
        }
    
    }
    
    InvClickListener class:
    Code:
    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
        }
    
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            String title = e.getInventory().getName();
            if (title.equals(ClassesUI.inv_name)) {
                e.setCancelled(true);
                if (e.getCurrentItem() == null) {
                    return;
                }
            
                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

    knokko

    @TsjipTsjip

    To make things easier, I got the codes from SimplePluginManager from https://github.com/Bukkit/Bukkit/bl...va/org/bukkit/plugin/SimplePluginManager.java.

    Code:
    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()) {
                getEventListeners(getRegistrationClass(entry.getKey())).registerAll(entry.getValue());
            }
    
        }
    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

    TsjipTsjip

    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.
     

Share This Page