Solved NullPointerException........

Discussion in 'Plugin Development' started by MOMOTHEREAL, Nov 5, 2013.

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

    MOMOTHEREAL

    Hello,
    doing some reorganisation in my plugin.
    I get a NullPointerException when an event gets fired.
    Here is my code:

    Main:
    Code:java
    1. public static PluginEvents events;
    2. [...]
    3. public void onEnable() {
    4.  
    5. PluginEvents.main = this;
    6. PluginEvents.config = this.getConfig();
    7. PluginEvents.server = this.getServer();
    8. [...]
    9.  
    10. }
    11.  
    12. [...]
    13.  
    14. @EventHandler
    15. public void onPlayerLeave(PlayerQuitEvent event) {
    16. events.onPlayerQuit(event);
    17. }
    18.  
    19. [...]


    PluginEvents:
    Code:java
    1. public static Main main;
    2. public static Server server;
    3. public static FileConfiguration config;
    4. [...]
    5. public void onPlayerQuit(PlayerQuitEvent event){
    6. Main.events = this;
    7. event.setQuitMessage("");
    8. }
    9. [...]


    The console doesn't even tell me where is the error :/
     
  2. Offline

    Mitsugaru

    In Main.java, your static variable PluginEvents events will never get initialized. Therefore, on the PlayerQuitEvent that you've registered, the method call on events is null and throws the NPE.

    You really shouldn't use static in this way... Can you do the following instead?
    Code:
    public PluginEvents events = new PluginEvents();
     
  3. Offline

    MOMOTHEREAL

    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page