Need help updating Config

Discussion in 'Plugin Development' started by UaVxChallenge, Sep 28, 2014.

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

    UaVxChallenge

    Okay so I am trying to make it so where people can log there birthday in a config file. However I keep getting a Null pointer Exception. I don't get what the problem is.

    Code:java
    1. Birthday plugin;
    2.  
    3. @Override
    4. public void onCommand(Player player, String[] args){
    5. player.sendMessage("Here");
    6.  
    7. if(args.length <= 4){
    8. MessageManager.getInstance().bad(player, "Not enough arguments");
    9. return;
    10. }
    11. plugin.getConfig().set("Players", "" + player.getName() + "");
    12. plugin.getConfig().set("Players." + player.getName() + ".Birthday", args);
    13. player.sendMessage("Try");
    14. plugin.saveConfig();
    15. plugin.reloadConfig();
    16.  
    17. return;
    18. }


    Its on line 11 in that.

    It connects to this:
    Code:java
    1. private void initializeConfig(){
    2. final FileConfiguration config = this.getConfig();
    3. config.addDefault("Players.UaVxChallenge.Birthday", "06/08");
    4.  
    5. config.options().copyDefaults(true);
    6. saveConfig();
    7. }
     
  2. Offline

    Totom3

  3. Offline

    UaVxChallenge

    Maybe I thought
    Code:java
    1. public Birthday plugin;
    2.  

    would work but it didn't so im not 100% sure any other way thats what I usually do
     
  4. Offline

    Totom3

    UaVxChallenge No, it wouldn't work because you didn't initialize it, only declared it. Because of that, plugin equals null and calling a method on it throws a NPE. Make a constructor where you initialize it.
     
  5. Offline

    UaVxChallenge

    Like this?
    Code:java
    1.  
    2. private static final Plugin plugin = Birthday.getPlugin();
    3.  
    4. @Override
    5. public void onCommand(Player player, String[] args){
    6. player.sendMessage("Here");
    7. if(args.length <= 0){
    8. MessageManager.getInstance().bad(player, "Not enough arguments");
    9. return;
    10. }
    11. plugin.getConfig().set("Players", "" + player.getName() + "");
    12. plugin.getConfig().set("Players." + player.getName() + ".Birthday", args);
    13. player.sendMessage("Try");
    14. plugin.saveConfig();
    15. plugin.reloadConfig();
    16.  
    17. return;
    18. }
    19.  


    Main Class:
    Code:java
    1.  
    2. public static Plugin plugin;
    3.  
    4. public static Plugin getPlugin(){
    5. return(plugin);
    6. }
     
  6. Offline

    Totom3

    UaVxChallenge That should work. Just remember to set plugin to null in onDisable(), to prevent memory leaks.

    Actually you have to set a value for plugin too. In the constructor of the main class, do something like plugin = this;
     
  7. Offline

    x17Andrew71x

    UaVxChallenge Throw this above your onCommand method.

    Code:java
    1. public cmdclassname(mainclassname instance) {
    2. plugin = instance;
    3. }


    You will still have to declare it as well:

    Code:java
    1. public mainclassname plugin;
     
  8. Offline

    UaVxChallenge

    It didnt work :/

    Im still getting it. Plus it made me change it in my CommandManager to Null

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page