Declaring plugin -- always is null?

Discussion in 'Plugin Development' started by EnvisionRed, Jun 23, 2012.

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

    EnvisionRed

    I need to fetch some config values in a class other than the main one, so I need to do something along the lines of
    Code:
    plugin.getConfig().getWhatever("Something", Something)
    
    However, whenever I declare the
    Code:
    public static pluginname plugin;
    I get nullpointer exceptions. Please help.
     
  2. Offline

    colony88

    Did you declare it in the main class?
     
    ferrybig likes this.
  3. Offline

    Milkywayz

    Code:
    public static pluginname plugin;
     
     
    @Override
    public void onEnable() {
    plugin = this;
    }
    When you don't set the "pluginname" to anything except a name -> plugin, then it will be null.
     
  4. Offline

    theguynextdoor

  5. Offline

    Sagacious_Zed Bukkit Docs

    When you declare any instance variable they will always have a default value, unless you assign the instance variable with a value the default value will remain.

    Also, avoid making any variable static. It is not necessary.
     
  6. Offline

    dxwarlock

    Or can do it how I do it..declare the variables globally in the main...
    Code:java
    1.  
    2. public class Main extends JavaPlugin implements Runnable {
    3. private static Main instance;
    4. public final Main playerListener = this;
    5. Lis lis = new Lis(playerListener);
    6. Logger log = Logger.getLogger("Minecraft");
    7. static List<String> msgs;
    8. static int msgCount = 0;
    9. static int currentMsg = 0;
    10. static int BreedChance = 0;
    11.  


    then load it as normal in the main onenable by reading the config.

    and call it in any class by doing Main.BreedChance or Main.msgCount etc..
    if you Main is called BobsAwesomeMainClass it would be BobsAwesomeMainClass.BreedChance
     
  7. Offline

    Sagacious_Zed Bukkit Docs

  8. Offline

    dxwarlock

    True much simpler, if I could figure out the right way to have multiple listener classes, and classes to put random one shot methods in and call them correctly, Id use that :)

    My Main, and Lis classes have ALL kind random methods in them not related to what they 'do' as far as class organization. because not sure how to move a method to say "conLoad.class" to call it and read the config, using it.

    I Guess my way I showed isn't really the best way, its more a "Not sure how to do it, so this works fine". but doesnt help in learning more...so ignore my above post Envision..haha
     
  9. Offline

    EnvisionRed

    That's a lot of replies. Seems like there are a lot of ways to do it. However,
    I don't think that is gonna work in an external class.
     
Thread Status:
Not open for further replies.

Share This Page