Solved NullPointerException for putting data in messages.yml

Discussion in 'Plugin Development' started by 9budddy, Jul 17, 2020.

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

    9budddy

    Code:java
    1.  
    2. private MessageFormat msgFormat;
    3. public static HashMap<String, String> messageData = new HashMap<String, String>();
    4.  
    5. @Override
    6. public void onEnable() {
    7. File f = new File(getDataFolder()+File.separator+"messages.yml");
    8.  
    9. if (!f.exists()) {
    10. try {
    11. getDataFolder().mkdir();
    12. f.createNewFile();
    13. } catch (IOException e) {
    14. e.printStackTrace();
    15. }
    16. }
    17.  
    18. msgFormat.setMessage("playerOnly", "&4Only a player can use this command.");
    19. msgFormat.setMessage("playerOffline", "&4That player is offline.");
    20. msgFormat.setMessage("noPermissionSelf", "&4You do not have permission to execute this command.");
    21. msgFormat.setMessage("noPermissionOthers", "&4You do not have permission to execute this command.");
    22. msgFormat.setMessage("nvEnabled", "&bNightVision has been &aEnabled.");
    23. msgFormat.setMessage("nvDisabled", "&bNightVision has been &4Disabled.");
    24. msgFormat.setMessage("nvEnabledBy", "&bYour NightVision was &aEnabled &bby {player}.");
    25. msgFormat.setMessage("nvDisabledBy", "&bYour NightVision was &4disabled &bby {player}.");
    26. msgFormat.setMessage("nvTargetEnabled", "&bYou &aEnabled &b{target}'s NightVision.");
    27. msgFormat.setMessage("nvTargetDisabled", "&bYou &4Disabled &b{target}'s NightVision.");
    28.  
    29. FileConfiguration config = YamlConfiguration.loadConfiguration(f);
    30. for (String message : config.getConfigurationSection("").getKeys(false)) {
    31. messageData.put(message, config.getString(message));
    32. }
    33. }
    34.  

    I get a NullPointerException Error here at:
    Code:
            msgFormat.setMessage("playerOnly", "&4Only a player can use this command.");
    
    Code:java
    1.  
    2. private FileConfiguration msgConfig;
    3.  
    4. public void setMessage(String name, String message) {
    5. File f = new File(getDataFolder()+File.separator+"messages.yml");
    6. msgConfig = YamlConfiguration.loadConfiguration(f);
    7. if (!msgConfig.isSet(name)) {
    8. msgConfig.set(name, message);
    9. try {
    10. msgConfig.save(f);
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }
    16.  


    I noticed that this works if I place the setMessage() function in the Main class with the onEnable(). I wanted to keep my setters and getters separate, so I made another class for it. Though, this NullPointerException error is throwing me off.
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    9budddy

    Thank you very much! Found out through a couple of tests and errors that you can't have two Initialization features of JavaPlugin. So I ended up sending the Main class through the function in order to gain access to the JavaPlugin without re-initializing it.

    For those that don't understand my solution from timtower's help here it is:
    Code:java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. private Plugin plugin;
    5. MessageFormat msgFormat = new MessageFormat();
    6. public static HashMap<String, String> messageData = new HashMap<String, String>();
    7.  
    8. @Override
    9. public void onEnable() {
    10. plugin = this;
    11. File f = new File(getDataFolder()+File.separator+"messages.yml");
    12.  
    13. if (!f.exists()) {
    14. try {
    15. getDataFolder().mkdir();
    16. f.createNewFile();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. msgFormat.setMessage(plugin ,"playerOnly", "&4Only a player can use this command.");
    22. msgFormat.setMessage(plugin, "playerOffline", "&4That player is offline.");
    23. msgFormat.setMessage(plugin, "noPermissionSelf", "&4You do not have permission to execute this command.");
    24. msgFormat.setMessage(plugin, "noPermissionOthers", "&4You do not have permission to execute this command.");
    25. msgFormat.setMessage(plugin, "nvEnabled", "&bNightVision has been &aEnabled.");
    26. msgFormat.setMessage(plugin, "nvDisabled", "&bNightVision has been &4Disabled.");
    27. msgFormat.setMessage(plugin, "nvEnabledBy", "&bYour NightVision was &aEnabled &bby {player}.");
    28. msgFormat.setMessage(plugin, "nvDisabledBy", "&bYour NightVision was &4disabled &bby {player}.");
    29. msgFormat.setMessage(plugin, "nvTargetEnabled", "&bYou &aEnabled &b{target}'s NightVision.");
    30. msgFormat.setMessage(plugin, "nvTargetDisabled", "&bYou &4Disabled &b{target}'s NightVision.");
    31. FileConfiguration config = YamlConfiguration.loadConfiguration(f);
    32. for (String message : config.getConfigurationSection("").getKeys(false)) {
    33. messageData.put(message, config.getString(message));
    34. }
    35.  
    Code:java
    1.  
    2. public class MessageFormat {
    3. private FileConfiguration msgConfig;
    4.  
    5. public void setMessage(Plugin plugin, String name, String message) {
    6. File f = new File(plugin.getDataFolder()+File.separator+"messages.yml");
    7. msgConfig = YamlConfiguration.loadConfiguration(f);
    8. if (!msgConfig.isSet(name)) {
    9. msgConfig.set(name, message);
    10. try {
    11. msgConfig.save(f);
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. }
    17.  
     
Thread Status:
Not open for further replies.

Share This Page