Solved Null?

Discussion in 'Plugin Development' started by The Fancy Whale, Apr 3, 2014.

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

    The Fancy Whale

    I am working on a plugin where I access the config in multiple classes. For some reason this line came up as null when I executed a command with a method that contains this:
    (Non-Main Class)
    Code:java
    1. FileConfiguration fc = plugin.getConfig();

    Same (Non-Main Class) I also use this code:
    Code:java
    1. static Main plugin;
    2.  
    3. public void ConfigListener(Main instance) {
    4. plugin = instance;
    5. }


    Is there something I have to do in the main class other than just creating the config?
    Any help is greatly appreciated thanks!
     
  2. Offline

    Not2EXceL

    Gotta question why you made your JavaPlugin instance static
     
    gyus likes this.
  3. Offline

    gyus

    Take off "static" and it should works. You are doing it well, passing the main object as parameter in the constructor and then calling .getconfig() method. Obviously, you have to create the Main object in the plugin Class by
    Main instance = new Main(this);
     
  4. Offline

    Not2EXceL

    No, the JavaPlugin instance should never be instantiated outside of Bukkit loading the plugin.
     
  5. Offline

    The Fancy Whale

    Not2EXceL gyus Wait now I am confused about how to fix this...

    Oh that's because I have a static method where I use plugin...
    How would I access it in a static method without the modifier of plugin being static?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Not2EXceL

    What method do you have in that class that must call on plugin yet still be static?
     
  7. Offline

    gyus

    No one said that man. Read well.

    The Fancy Whale
    Let me explain:

    Let's call your main class (that who inherit from Java Plugin) CLASS A and another class that you want to acces to the main class CLASS B.

    In the CLASS B you nedd an attribute of CLASS A. Then, on his constructor you ask for a CLASS A parameter.

    A piece of code would be better


    public class classB {

    classA mainClass;

    public classB(classA mainClassInstance) {
    this.mainClass = mainClassInstance;
    }
    }



    Now you can use any method of your main class from classB usinf mainClass attribute. Solved?
     
  8. Offline

    xTigerRebornx

    Actually, Bukkit throws an error when you try to create a new instance of any class that extends JavaPlugin improperly (and the code example you provided does it improperly)
     
  9. Offline

    skyrimfan1

    Include this in your plugin's main class (of course, substitute the YourPluginName with your plugin's actual name)
    Code:
    public static YourPlugin getInstance(){
        return (YourPlugin) Bukkit.getPluginManager().getPlugin("YourPluginName");
    }
    And then you can just call:
    Code:
    YourPlugin.getInstance()
    To retrieve the singleton instance of the plugin.

    Not bad either but personally I hate plugin references within constructors; they just look really messy to me.
     
  10. Offline

    xTigerRebornx

    skyrimfan1 I was not referring to the OP's code, nor the valid code, but this code
    Code:
    Main instance = new Main(this);
    Where the class Main is something that extends JavaPlugin.
     
  11. Offline

    skyrimfan1

    xTigerRebornx Oh, that would still be an invalid creation of a duplicate singleton; plus, Bukkit wouldn't initialize the created plugin instance.
     
  12. Offline

    xTigerRebornx

    skyrimfan1 Exactly, and which I said, it will throw an error.
     
  13. Offline

    skyrimfan1

    xTigerRebornx Ah my bad. I wasn't clear earlier as to what you were referencing. :p
     
  14. Offline

    gyus

    xTigerRebornx

    I know how to program a plugin ;) Maybe you have not undesrtood what i told. Check it out.

    skyrimfan1

    I prefere plugin references, but obviously anyone have it's own gouts :)
     
  15. Offline

    Not2EXceL


    Either you said that, or you clearly dont know how to explain anything. Its passing the plugin instance to the object through the constructor. Not w/e the fuck you said, which makes 0 sense.

    edit: I know why you put that constructor now. The fact that you used the naming convention Main fucked everyone reading it over. Also its not called just creating the Main object in the plugin class, its instantiation, and passing variables/objects to constructors.


    Actually you shouldn't make repeated calls to getPlugin for a singleton, its very inefficient. Bukkit instantiates JavaPlugin on load, and you can set an instance variable onEnable or w/e else you wish. Then just have the singleton reference that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  16. Offline

    gyus

    Not2EXceL

    I called Main because the author of this thread called it Main. Read the first post. I know that is confusing, but i was answering to the author, who had the doubt. However, it seems that people have some interest correcting others to prove how good they are... sad fact :(
     
  17. Offline

    Not2EXceL

    I misread your first reply due to the fact that it was name Main, hence the confusion on many people's parts. Its not that we have interest correcting others, its that people commonly make that mistake here despite it being basic knowledge (not implying you, but its happened before). Theres lots of things that get posted here, that can simply be solved by google or learning the fundamentals of java, or if the person doesnt wish either, the search here has a ton of things that get repeatedly asked.
     
    gyus likes this.
  18. Offline

    skyrimfan1

    Alright, thank you for that tip!
     
Thread Status:
Not open for further replies.

Share This Page