NullPointerException: Scoreboard

Discussion in 'Plugin Development' started by ShadowLAX, Oct 18, 2013.

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

    ShadowLAX

    Hello bukkit, I am having a strange problem. When ever I use; Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard(); It always throws a NullPointerException on startup, and when I reload the server it works fine. I don't know what is wrong on the startup, maybe there is something that should go in the onEnable() method?

    Thanks!
     
  2. Offline

    lycano

    Taken from the Javadocs

     
  3. Offline

    ShadowLAX

    lycano ahhh. That explains it. Any idea how to fix if possible?
     
  4. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

  5. Offline

    ShadowLAX

    @mbaxterI do have it called after my onEnable(),

    Code:java
    1. public void onEnable() {
    2. getLogger().info("has been enabled!");
    3. Bukkit.getServer().getPluginManager().registerEvents(new Listener(this), this);
    4. }
    5.  
    6. public void onDisable() {
    7. getLogger().info("has been disabled!");
    8. }
    9.  
    10. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    11.  
    12. //rest of my code


    This should work, right?
     
  6. Offline

    lycano

    ShadowLAX xD *caugh* visually spoken it is defined after onEnable() but technically its initialized when the class (Your Plugin) gets loaded by the class loader as you defined it as class variable.

    So "board" is initalized before onEnable() is even called.

    change your code to:

    private Scoreboard board;

    public void onEnable() {
    this.board = Bukkit.getScoreboardManager().getNewScoreboard();
    }

    and it should work.

    Note board is visually placed before onEnable doesnt matter here could also be placed after it (just coding style) what matters is that you do not initalize the Object board before onEnable() which is called by the Bukkit PluginLoader.
     
Thread Status:
Not open for further replies.

Share This Page