[SOLVED] Null config file when referencing Main class

Discussion in 'Spigot Plugin Development' started by MckenzieSkullKid, Jun 22, 2015.

  1. When trying to reference my config file from a separate class I am unable to actually get my config file and am receiving an NPE when i attempt to get the config.

    I have made sure that the config is setup and saved within my onEnable however I am still having issues, is anyone able to help me?

    Null Pointer Exception:

    Main Class:

    MapManager Class:

    Join Class:
    Not required, only references Post Enabled method in Main class using plugin.getConfigFile() as the arguments
  2. If you'd've read that stacktrace, you'd be aware that it isn't actually an issue with the config.
  3. It's erroring on line 26 of MapManager which is:
    FileConfiguration config = plugin.getConfigFile();
  4. Look at the very first line of the trace
  5. Moved Post Enable from onJoin to enabling of server and get the same error

  6. Maybe this helps:

    Code (Text):
        public File ActieFile = new File(getDataFolder()+"/Actiefoto.yml");
        public FileConfiguration ActieData = YamlConfiguration.loadConfiguration(ActieFile);
    Code (Text):
      public void saveAfFile(){
          try {
          } catch (IOException e) {
  7. Code (Text):
     getServer().getPluginManager().registerEvents(PlayerInteract, this);
    Is not how you register events.
  8. Code (Text):
      public void onEnable()
          PluginManager pm = Bukkit.getServer().getPluginManager();
          pm.registerEvents(this, this);
          pm.registerEvents(new Class(), this);
  9. He could've figured that out himself from my link. Please don't spoonfeed people, it teaches them nothing.
  10. if you have a public static Main plugin in your Main why not set this.plugin = this; after you register and reference that in your other classes by just doing Main.plugin.getConfig() or whatever method you would need from Main.plugin. This way you don't have a bunch of static instances of main, some of which may not have a instances that have a config. and then what he says:
    you did public final Join Join = new Join(this); (by the way it should be public final Join join;) You are sending an instance of main way before your config is set up and im guessing referencing that instance when a player joins. Reference your public static instance of Main instead or do your events like getServer.getPluginManager().registerEvents(new Join(this), this);
  11. Main.plugin.getConfig() != OOP; Use proper class access and, getter/setter methods.
    Then pass the instance to the constructor in your class.
  12. I completely agree, there were multiple things wrong with his code but I figured if he was going to use a public static variable might as well make it so he understands what im saying. I personally do it differently but I'm just trying to help him get his code working.
  13. @Fearmig - Why not teach him the right way?
  14. Tooooo much typing, and not quite sure he would understand. if i was in a personal setting it would be different. But to solve this here and now:

    @MckenzieSkullKid what you should (if you really need to use a static instance of Main)do is make in you Main a private static Main plugin; then have getter and setter methods for that variable that you can call from your other classes (or just pass the correct instance to them in their constructors) to get a current version of plugin. Now really I wouldn't have a static instance of Main unless you really need to, in general if you can prevent the use of static variables it will be better on memory usage and such. Most of the time entry-level programmers don't really understand what static really is and what cases it should be used in. Like in this example, from what I have seen you don't really need a static instance of main in the first place if you properly set up your listeners, and you definitely don't need to have a static instance in multiple classes.

    Is that better @BlackDawn187? :p
    • Like Like x 1
  15. Thank you all for your help (@Fearmig , @Danique )
    It is working perfectly now. I will also refactor all of my code in line with what you have said @Fearmig

    Thanks for the help