Making an object for getConfig()

Discussion in 'Plugin Development' started by bigbeno37, Jul 12, 2012.

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

    bigbeno37

    Hey there!

    I am trying to create an object for getConfig() for ease of access from the class that defines it and outside classes. I am using the following code

    Code:
    public FileConfiguration config = getConfig();
    However, when starting the server, it says that I cannot make an object equal to null. In this case it is referring to 'config'. However I have set that to getConfig(), and thus I am unaware as to what I should do to fix it.

    [EDIT]
    Should I put it under onEnable() to kind of 'start' the getConfig()? Or what should I do?
     
  2. Offline

    EnvisionRed

  3. Offline

    bigbeno37

    So I shouldn't bother about making an object? In that case, would it be possible to use getConfig() from outside classes? Or would I need to setup the variables and the like inside the main class, and use them from there?

    [EDIT]
    I have another question. When we create an oject for other classes, instead of using constructors, couldn't we use:

    Code:
    private MyMainClass main = new MyMainClass();
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    bigbeno37
    Either English is not your first language or you are very new to programming. But you are using the wrong terminology for the wrong concepts. What you have described is a instance variable declaration and assignment. An object is an instance of a class, created by a constructor. They are what variables point to, and multiple variables can point to the same object.

    The wiki advises against creating an instance variables because once it is assigned to point to a particular object, it will not point to a newer FIleConfiguration object if one is created. Also as a special note, you cannot call getConfig before your plugin is initialized. So the safest place is to put an local variable assignment in onEnable and any Listner or CommandExecutor methods.

    As long as you have a pointer to an instance of JavaPlugin you can call getConfig() on it, as it is an instance method. Therefore, it is not necessary to duplicate it with your own methods.

    No, you cannot construct a new instance of MyMainClass. It will not be in the correct state and it WILL cause problems for you, and the plugin will not function correctly. Again the advice is to not create new instance variables pointing to the configuration object, because there might be a different object. In this case you need a variable pointing to your main class and you can then invoke getConfig.
     
  5. dont get the config before onEnable is called, before that point it wil be null
     
  6. Offline

    bigbeno37

    Okay, thanks for that. So to make an object of another class, could I simply use 'MyPluginListener listener = new MyPluginListener();'?
     
Thread Status:
Not open for further replies.

Share This Page