UPDATE: Config Issues from another class

Discussion in 'Plugin Development' started by MasterDoctor, Nov 2, 2015.

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


    I have set up a constructor for my config in my listener class, I have added in the right sections into the config, I have even checked the config generated - it generated correctly.
    I still get a NullPointerException whenever I try and use the config with
  2. Online

    timtower Moderator Moderator

  3. Offline


    HA. I know the problem is at line 26 of the "main" class. the field "plugin" is null!

    I have no clue about anything related to your code. Could you post your code so we can see what your problem actually is?
  4. Offline


    Sorry about my tiny post guys, I was in a huge rush :p
    Here is the necessary part of the Main Class:
    PluginDescriptionFile pdfFile = this.getDescription();
        public static Main instance;
        public void onEnable() {
            Main.instance = this;
            ConsoleCommandSender console = Bukkit.getConsoleSender();
            // Save default config
            try {
            } catch (Exception e) {
                console.sendMessage(ChatColor.RED + "AN ERROR HAS OCCURED IN PLUGIN " + pdfFile.getName().toUpperCase()
                        + " v." + pdfFile.getVersion() + ":" + " " + "The configuration file failed to save, please send the following to the plugin author:");
            //Register new listener
            new ListenerClass(this);
    Here is the necessary part of the ListenerClass;

    public class ListenerClass implements Listener {
        Main plugin;
        public ListenerClass(Main instance) {
            plugin = instance;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        String prefix3 = plugin.getConfig().getString("chat.prefix");
        String prefix2 = prefix3 + "ChatColor.RESET" + " ";
        String prefix = ChatColor.translateAlternateColorCodes('&', prefix2);
    The error is on the line:
    String prefix3 = plugin.getConfig().getString("chat.prefix");
    Again, Sorry about my tiny post.

    EDIT: This string has been defined in the config.
  5. Offline


    You placed it outside....why....why not inside the constructor?....

    When you run the code, then the vm usually places everything on the class stage to the top of the file, so it first creates a null variable, then it tries to get the string from the yml file, and just then run the contructor. Learn a bit more about how java files run.

    Edit: you waste memory with the 3 different string variables. Just use one.
    Last edited: Nov 3, 2015
    Zombie_Striker likes this.
  6. Offline


    I wrote it inside the constructor because that's what I learnt from a tutorial.
    EDIT: This same code worked for me before.
  7. Offline


    No you wrote it outside the constructor. Just look at the brackets.

    Edit: This can be easily repaired if you create a global variable out of the prefix, and place the getting code inside the constructor.
    Last edited: Nov 3, 2015
    Zombie_Striker likes this.
  8. Offline


    Why inside the Constructor though?
    My problem is with reading from another class - from the Main class it reads fine.
  9. Offline


    I want to tell you something. This may blow your mind, so get ready.

    Instances created outside methods are created BEFORE instances in methods.

    Crazy, right? So you're getting the plugin's instance before it has even been created. So, what's happening is that you're getting the config from a null (not even from a class at this point, from a null),which is why it doesn't work.
    MasterDoctor likes this.
  10. Offline


    I'm pretty sure this didn't work for you before. And I'm also concerned, that you don't know what the constructor is.

    This is what the VM sees:

    Main plugin;
    String prefix = plugin.getConfig().getString("chat.prefix");
    <other methods with prefix>  // These methods got moced here right after the start.
    public ListenersClass(Main instance) {
        this.plugin = instance;
    Every bigger programming language does this (java; C#; C++; hell, even javascript), so it doesn't need to free up space in the memory for global variables later in the code.
    Last edited: Nov 4, 2015
  11. Offline


    No, I badly phrased what I said - I do know what a constructor is.
    Ok, I get it know - Sorry, I was in a rush again and didn't properly read or think about what you said :p
    Anyway, I get it know.

    I'm so stupid :)
    That is literally all I can say :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Nov 5, 2015
  12. Offline


    So is this issue resolved?

    If so, can you mark the thread as solved by Editing the title above your original post
Thread Status:
Not open for further replies.

Share This Page