Solved File cannot be null

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

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

    MOMOTHEREAL

    Hello, I am doing some reorganisation in my big plugin.
    I am placing all events in a specific class, called PluginEvents.
    Before, I created a thing called setGameState(...). It worked super well.
    Now when the onPlayerJoin method in the PluginEvents class gets fired up, it needs to fire up the setGameState thingy.
    The codes:
    Main.setGameState:
    Code:java
    1. public void setGameState(Player player, String gamestate, FileConfiguration config){
    2. config.set("players."+player.getName()+".GameState", gamestate);
    3. this.saveConfig();
    4. this.reloadConfig();
    5. }

    PluginEvents.onPlayerJoin:
    Code:java
    1. public void onPlayerJoin(Player playerJoined, FileConfiguration config, PlayerJoinEvent event, Server server){
    2. Main main = new Main();
    3. [...]
    4. main.setGameState(playerJoined, GameStates.GENERAL_SPAWN, main.getConfig());
    5. [...]
    6. }


    And now the problem:
    I get an error in the console telling me that the config file in the setGameState() is null. Which doesn't make sense since in the onPlayerJoin it sais that it is main.getConfig(). So main.getConfig() = null?????
     
  2. Offline

    adam753

    You can't create a new instance of your main class and expect it to have access to things like the config. The Bukkit plugin manager will create an instance of your main class when the plugin is enabled, and methods like getConfig() will only work for that instance.
     
  3. Offline

    MOMOTHEREAL

    So what do you suggest as an alternative? I also program in Forge and I see alot of similar code.​
     
  4. Offline

    adam753

    MOMOTHEREAL
    You need a reference to your main class instance in any other classes you use. This is the easiest/laziest way to do it:

    Code:java
    1.  
    2. //Put this at the top of your class (let's call it the MyListener class):
    3. public static Main main;
    4.  
    5. //Put this in main's onEnable:
    6. MyListener.main = this;
    7.  
    8. //When you want to get the config from MyListener:
    9. main.getConfig()
    10.  
     
  5. Offline

    MOMOTHEREAL

    ~Dancing on chair~
    Thanks a lot for your help!
     
Thread Status:
Not open for further replies.

Share This Page