Solved NullPointerException

Discussion in 'Spigot Plugin Development' started by Artourious, Jan 23, 2020.

  1. When I try to start my server with my plugin I get this error. I know it has something to do with line 12 but im not sure how to fix it.

    Main Code

    Code (Java):
    package me.artourious.compasslocator;

    import org.bukkit.event.Listener;

    import me.artourious.compasslocator.PlayerQuitListener;

    public class Main extends JavaPlugin implements Listener{

           private Main plugin;
        public void onEnable(){
           plugin.getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this);

    Code (Java):
    name: CompassLocator
    version: 1.0
    author: Artourious
    main: me.artourious.compasslocator.Main
    description: My Plugin
  2. You never set the instance to your Main class. You declared the variable but never assigned a value. In your onEnable() add:

    plugin = this;
  3. Like this?

    Code (Java):
    public void onEnable(plugin=this);
  4. No that would be adding an invalid parameter to the method. I have a feeling you don't understand java. I would suggest you read up on it while programming.

    public void onEnable() {
    plugin = this;

    #4 SeniorCluckers, Jan 23, 2020
    Last edited: Jan 23, 2020
    • Agree Agree x 1
    • Winner Winner x 1
  5. Strahan


    Not sure why you threw the static keyword in there though.

    I'd also add that since you are registering your listeners to the main class, I assume this is going to be a one class plugin. If that's the case, there is no need to make a plugin instance anyway.
  6. Whoops. I'm in class and I accidentally mixed information. Thanks for that catch.