Solved Getting a NullPointer Exception

Discussion in 'Plugin Development' started by Betagear, Sep 7, 2015.

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

    Betagear

    Hi.
    I'm getting a null pointer exception when reading from config.yml in a not main jar.
    Extracted code : Main class (Main.java) :
    Code:
        PlayerListener Playerlistener = new PlayerListener(this); //Here is an error
       
        @Override
        public void onEnable() {
            plugin = this;
           
            saveDefaultConfig();
           
            }
        }
    Secondary class (PlayerListener.java) :
    Code:
        Plugin plugin = (Plugin) Bukkit.getPluginManager().getPlugin("StompArena");
        boolean ActivateWorldRestriction = plugin.getConfig().getBoolean("General.World.ActivateWorldRestriction"); //Here's the error
        double ExplosionDemultiplier = plugin.getConfig().getDouble("Balance.ExplosionDemultiplier");
    
        public PlayerListener(Main plugin) { //Constructor
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
    I putted only in the thing that the console says me there's a problem.
     
  2. Offline

    RoboticPlayer

    Which is the class that extends JavaPlugin? If it's not PlayerListener, move the registration of events to your main class's onEnable. Secondly, follow Java Naming Conventions (don't use "Main" for your main class).
     
  3. Offline

    Betagear

    PlayerListener has "implements Listener" and Main.java (I'll rename it later) has "extends JavaPlugin", if that's what you need.
     
  4. Offline

    RoboticPlayer

    I haven't ever seen registering the plugin in a way that you did it, so I don't know if it works. But this is what I do:
    Code:
        private /*Main class*/ plugin;
    
        public /*Other class*/(/*Main class*/ plugin) {
            this.plugin = plugin;
        }
     
  5. Offline

    Betagear

    Ok I did it, but then how can I acces plugin in anything other than the constructor ?
     
  6. Offline

    RoboticPlayer

    The way I have always done it, I register my config values in my onEnable, but I'm not the most experience with Bukkit, still learning as well.
     
  7. Offline

    Hawktasard

    @Betagear
    Well, I'm pretty sure you can't register a listener before your plugin is enabled.
    edit: Registering the listener when your plugin is enabled probably won't fix your NPE though, you should post the error for us to see what's wrong.
     
  8. Offline

    Oxyorum

    @Betagear Assuming you did what @henderry2019 said, just use the private reference to your main plugin class to things like:
    Code:
    plugin.methodFromMyPluginClass()
    in your secondary class.

    Also, the NPE is thrown because there is no configuration file in your JAR for the saveDefaultConfig() method to save to disk (that's what saveDefaultConfig() does). Also, you must check to see if your plugin's data folder exists and create it if it doesn't, since that's where your default config would be saved to anyway.

    Edit: Read Configuration API Reference.
     
    Last edited: Sep 7, 2015
  9. Offline

    teej107

    @Betagear This is the reason why I only initialize non-constant fields inside the constructor (or onEnable() in this case)
     
  10. Offline

    Betagear

    Now I have another problem : Plugin attempted to register while not enabled. What should I do ? Move the order of execution ?

    I solved it : Simply change PlayerListener Playerlistener =new PlayerListener(this); to PlayerListener Playerlistener; and then put Playerlistener =new PlayerListener(this); in the OnEnable.

    Thanks you all for your help !

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  11. Offline

    Tecno_Wizard

    @Betagear, please mark the thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page