Solved Plugin always null in any classes other than default?

Discussion in 'Plugin Development' started by Simo389, Sep 21, 2013.

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

    Simo389

    Ok, so I have 2 classes. One main one and a second with some methods in.

    The main class looks like this:
    Code:java
    1. package me.Simo389.PandoraCTF;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.plugin.PluginDescriptionFile;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class PandoraCTF extends JavaPlugin {
    9.  
    10. //public static PandoraCTF plugin;
    11. PandoraCTF p = this;
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13.  
    14. @Override
    15. public void onDisable() {
    16. PluginDescriptionFile pdfFile = this.getDescription();
    17. p.logger.info(pdfFile.getName() + "version " + pdfFile.getVersion() + " is now disabled.");
    18. }
    19.  
    20. @Override
    21. public void onEnable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. p.logger.info(pdfFile.getName() + "version " + pdfFile.getVersion() + " is now enabled.");
    24.  
    25. ArenaSetup.getInstance().createArena();
    26.  
    27. getConfig().options().copyDefaults(true);
    28. saveDefaultConfig();
    29.  
    30. getCommand("actf").setExecutor(new CommandHandler());
    31. }
    32. }
    33.  


    So the issue is arising in this 'createArena' method.

    Here's what the class it's in looks like:
    Code:java
    1. package me.Simo389.PandoraCTF;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.Plugin;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class ArenaSetup extends JavaPlugin {
    14.  
    15. private static ArenaSetup instance = new ArenaSetup();
    16. private PandoraCTF p;
    17. private FileConfiguration arenas;
    18. private File arenasFile;
    19.  
    20. private ArenaSetup() {
    21. }
    22.  
    23. public static ArenaSetup getInstance() {
    24. return instance;
    25. }
    26.  
    27. public Plugin getPlugin() {
    28. return p;
    29. }
    30.  
    31. public void createArena(CommandSender sender, String[] args) {
    32. createDefaultArenaFile();
    33. }
    34.  
    35. public void createDefaultArenaFile() {
    36.  
    37. if (p == null) {
    38. System.out.println("[CTF] Plugin Is Null");
    39. }
    40.  
    41. //Create arenas.yml file if it doesn't already exist
    42. arenasFile = new File(p.getDataFolder(), "arenas.yml");
    43. try {
    44. if (!arenasFile.exists()) {
    45. arenasFile.createNewFile();
    46. }
    47. }
    48. catch (Exception e) {
    49. e.printStackTrace();
    50. }
    51. }
    52. }
    53.  


    And here is the error I'm getting:
    I added the 'Plugin is Null' message just to show what the issue is clearly, but I just can't work out how correct it.

    Any ideas?
     
  2. Offline

    bensku

    Try not extending JavaPlugin with your second class.
     
  3. Offline

    Flyverse

    1.
    "private PandoraCTF p = this" - Wait with the assignement until the onEnable method.
    2.
    "getInstance(){ return instance }" - Nope, a singleton does not works that way ^^ http://en.wikipedia.org/wiki/Singleton_pattern

    *Continues reading*
     
  4. Offline

    Simo389


    Ok, I've just tried that and it didn't help. :(


    What do you mean here sorry? You mean remove that completely from the ArenaSetup class and put what you mentioned in my main class in onEnable? (On doing this I get an illegal modifier error for p.)

    From reading that link, what I've done is correct? I'm currently using 'eager initialization' according to it.

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

    Flyverse

    (Oh gawd, I HATE the bukkit-forums-texteditor... I HATE him -_- )

    To 1:
    Change "private PandoraCTF p = this" to "private PandoraCTF p = null" and put "p = this;" in your onEnable. (Even if I don't understand why do you do this... You never use it in your code.)

    To 2:
    Uuh, thats a bit complicated to explain. I posted the wikipedia link, because it was explained very good there =S

    3:
    You never seem to initialize "p" in your "ArenaSetup" class - Could you please post the code where you do it? Because thats the issue: Your "p" is null. (Put the rest in the code in a else-clause: You check if p is null, but continue with your code anyway...)


    SOrry if I'm bad at explaining - I can't speak english very good Dx
     
  6. Change
    Code:java
    1.  
    2. PandoraCTF p = this;
    3.  

    to
    Code:java
    1.  
    2. PandoraCTF p;
    3.  


    add this to onEnable()
    Code:java
    1.  
    2. p = this;
    3.  


    ad this to onDisable()
    Code:java
    1.  
    2. p = null;
    3.  


    add this to main class
    Code:java
    1.  
    2. public static PandoraCTF getInstance(){
    3. if(p == null)p = this;
    4. return p;
    5. }
    6.  
     
  7. Offline

    Simo389


    Thanks for the help!

    For 1: Not sure what you're on about here? I don't have that in my onEnable?

    2: Changed my previous answer a bit for this.

    3: What you see is my full code. Initializing p is what I can't figure out. How do I do this in my ArenaSetup class? The if (p == null) was simply to show you the issue. :)


    Erm, think you have a typo here? Not sure if you can leave out brackets in Java like you can in something like PHP. You probably can and I'm just being a noob! :p

    But I added what I think you meant:
    Code:java
    1. public static PandoraCTF getInstance(){
    2. if(p == null) {
    3. p = this;
    4. return p;
    5. }
    6. }

    But I'm getting an error telling me that I can't make a static reference to p because it's non-static.

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

    zombiekiller753

    I didn't really read the above, so if there's anything repeated, sorry.

    So, here are my suggestions.

    1. These lines of code in onEnable and onDisable are just redundant
    Code:
    this.logger.info(pdfFile.getName() + "version " + pdfFile.getVersion() + " is now enabled.");
    Bukkit logs the fact that something is enabled. This is just console spam :p

    2. This isn't really a mistake, but I'll mention it anyways. Personally, it's easier for me to have all base commands ("/blah blah") in separate class files. That way, you just need to check for arguments.

    3. Don't extend JavaPlugin in ArenaSetup. It doesn't work (I've tried it back then :p)

    4. I'm not sure if this will work, but because the plugin variable is static, why are the methods non-static? Namely, createArena and createDefaultArena
     
  9. make it static.
     
  10. Offline

    newboyhun

    RealmsofValendor
    Come one he can't use 'this' in a static context......

    Simo389
    Initialize p field 'onEnable' and not in a static context.
    Maybe you don't know what the static fields are for.
     
  11. Offline

    Simo389

    zombiekiller753

    Thanks for all the suggestions!

    Had no clue about the startup messages and extends java stuff. :p


    My aim is to keep each class seperate, so for particular tasks I can go to the associated class. But having all of my methods static wouldn't achieve this. Essentially all I'm trying to do is make my methods in 'ArenaSetup' available from the main class and things like my CommandHandler. Hopefully what I have so far is along the correct lines. :)

    EDIT:
    Here's my latest code:
    Main class: http://pastebin.com/ffQQVLWn
    ArenaSetup class: http://pastebin.com/Bsd8J

    I'm just not sure exactly how to initialise plugin in the ArenaSetup class correctly for the above behaviour. Any help would be appreciated! :)


    I've added what you said. I'm aware that static allows accessibility from other classes, but I don't want this for every single method. That's why I'm trying to create the plugin instance in ArenaSetup. I just can't seem to work out how. :p

    My latest attempt is in the previous post. As far as I'm aware, I'm initialising it correctly; but p is still null at:
    Code:java
    1. arenasFile = new File(p.getDataFolder(), "arenas.yml");


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

    Janmm14

    Simo389
    ArenaSetup:
    remove lines 26-28 and 15
    change every p.*** to PandoraCTF.p.***

    PandoraCTF:
    remove line 12
    Change line 11 to public static PandoraCTF p;
     
  13. Offline

    Simo389


    I've just done all this, so now my main class looks like:
    Code:java
    1. package me.Simo389.PandoraCTF;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class PandoraCTF extends JavaPlugin {
    6.  
    7. PandoraCTF p;
    8.  
    9. public static PandoraCTF p(){
    10. if(p == null) {
    11. p = this;
    12. return p;
    13. }
    14. }
    15.  
    16. @Override
    17. public void onDisable() {
    18. p = null;
    19. }
    20.  
    21. @Override
    22. public void onEnable() {
    23. p = this;
    24.  
    25. ArenaSetup.getInstance().createArena(null, null);
    26.  
    27. getConfig().options().copyDefaults(true);
    28. saveDefaultConfig();
    29.  
    30. getCommand("actf").setExecutor(new CommandHandler());
    31. }
    32. }
    33.  


    But p isn't static, so I'm getting errors. Should I change 'PandoraCTF p;' to 'static PandoraCTF p;'? I assumed that doing something like this was bad practice.
     
  14. Offline

    newboyhun

    Simo389
    Yes, you should change it to static.
    Code:
    public static PandoraCTF p(){
    if(p == null) {
    p = this;
    return p;
    }
    }
    So again, 'this', what do you think ,what is the 'this'?
    You used 'this' in static context, AGAIN.
     
  15. Offline

    Janmm14

    Simo389
    Now change line 7 to
    static PandoraCFT p;

    And remove line 10 and line 11.
     
  16. Offline

    Simo389

    Thanks for all the help guys! After all of your suggestions I managed to get everything working correctly. :)
     
  17. Offline

    Hugs

    Yes you can, as long as it's just one line or one line & an else.
     
  18. Offline

    1Rogue Retired Staff

Thread Status:
Not open for further replies.

Share This Page