Solved NullPointerException error

Discussion in 'Plugin Development' started by KarimAKL, May 3, 2018.

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

    KarimAKL

    @timtower If i do this:
    Code:Java
    1.  
    2. System.out.println(plugin.messages);
    3.  

    It sends this:
    Code:
    [15:57:22 INFO]: cmd.getName
    [15:57:22 INFO]: !sender instanceof Player
    [15:57:22 INFO]: YamlConfiguration[path='', root='YamlConfiguration']
    
    With no errors.
     
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You didn't follow the steps, again.
     
  3. Offline

    KarimAKL

    @timtower If i do this:
    Code:Java
    1.  
    2. System.out.println(plugin);
    3.  

    It comes with the plugin name and the plugin version. If that is not what you meant then what did you mean?
     
  4. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Do both, print everthing, then print the string!
     
  5. Offline

    KarimAKL

    @timtower So like this?:
    Code:Java
    1.  
    2. System.out.println(plugin);
    3. System.out.println(plugin.messages);
    4. System.out.println(plugin.messages.getString("World.player-only"));
    5.  

    Or did i misunderstand that, again?
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    KarimAKL

    @timtower I see, i'll try it now then.
    EDIT: This is the result:
    Code:
    >world
    [16:35:04 INFO]: cmd.getName
    [16:35:04 INFO]: !sender instanceof Player
    [16:35:04 INFO]: WorldTeller v1.0
    [16:35:04 INFO]: YamlConfiguration[path='', root='YamlConfiguration']
    [16:35:04 INFO]: null
    
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    KarimAKL

    @timtower Uhm.. My string doesn't exist? But.. I have it in the messages.yml file. :/ Right here:
    Code:
    World:
      player-only: "Message here"
    
     
  10. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Print all keys that are in the config.
     
  11. Offline

    KarimAKL

  12. Online

    timtower Administrator Administrator Moderator

  13. Offline

    KarimAKL

    @timtower Would that before it sends this?:
    Code:
    [16:35:04 INFO]: null
    
     
  14. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Before it crashes and throws the actual error...
     
  15. Offline

    KarimAKL

    @timtower But it doesn't send the error when i do this:
    Code:Java
    1.  
    2. System.out.println(plugin);
    3. System.out.println(plugin.messages);
    4. System.out.println(plugin.messages.getString("World.player-only"));
    5.  

    Only the "null"
     
  16. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Because or the chat util, or the sendMessage can't handle null.
    But it is null, that needs to be fixed.
    Find out if world exists in the config.
     
  17. Offline

    KarimAKL

    @timtower What do you mean "if world exists in the config"? Like if the "World:" part is in the messages.yml? Also this is the chat util:
    Code:Java
    1.  
    2. public class ChatColorUtil {
    3.  
    4. public static String chat (String s) {
    5. return ChatColor.translateAlternateColorCodes('&', s);
    6. }
    7. }
    8.  

    That shouldn't cause any problems, right?
     
  18. Online

    timtower Administrator Administrator Moderator

    @KarimAKL It will, it will probably try to call replace on something that doesn't exist
     
  19. Offline

    KarimAKL

    @timtower But i've used it alot in my other plugins and in other classes etc and haven't had any problems with it, so why would it be a problem now? :/ Anyway, what should i do for this to work? Any idea?
     
  20. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Check if the config contains World, then check for the rest of the string.
     
  21. Offline

    KarimAKL

    @timtower Okay, i'll try now.
    EDIT: Is this how i'm supposed to do it?:
    Code:Java
    1.  
    2. @Override
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if (cmd.getName().equalsIgnoreCase("world")) {
    5. System.out.println("cmd.getName");
    6. if (!(sender instanceof Player)) {
    7. System.out.println("!sender instanceof Player");
    8. if (plugin.messages.getString("World") != null) {
    9. System.out.println("World != null");
    10. if (plugin.messages.getString("World.player-only") != null) {
    11. System.out.println("World.player-only != null");
    12. System.out.println(plugin);
    13. System.out.println(plugin.messages);
    14. System.out.println(plugin.messages.getString("World.player-only"));
    15. return true;
    16. } else {
    17. plugin.messages.set("World.player-only", "&cOnly players may execute this command!");
    18. return true;
    19. }
    20. } else {
    21. System.out.println("World null");
    22. plugin.messages.set("World", "World");
    23. if (plugin.messages.getString("World.player-only") != null) {
    24. plugin.messages.set("World.player-only", "&cOnly players may execute this command!");
    25. System.out.println(plugin);
    26. System.out.println(plugin.messages);
    27. System.out.println(plugin.messages.getString("World.player-only"));
    28. return true;
    29. }
    30. }
    31. } else {
    32. System.out.println("else (sender is player)");
    33. Player p = (Player) sender;
    34. String world = p.getWorld().getName();
    35. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("World.message").replace("{world}", world)));
    36. System.out.println("World message");
    37. return true;
    38. }
    39. }
    40. return false;
    41. }
    42.  

    I haven't changed or added anything to the player part because you said to focus on the console one for now.
    EDIT2: I was trying a few things out and then i fixed the problem, this is how:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public File messagesFile = new File(this.getDataFolder() + "/messages.yml");
    5. public FileConfiguration messages = YamlConfiguration.loadConfiguration(messagesFile);
    6.  
    7. @Override
    8. public void onEnable() {
    9. if (!getDataFolder().exists()) {
    10. getDataFolder().mkdirs();
    11. }
    12. if (!messagesFile.exists()) {
    13. this.saveResource("messages.yml", false);
    14. try {
    15. this.messages.load(messagesFile);
    16. } catch (IOException | InvalidConfigurationException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. new CommandWorld(this);
    21. }
    22. }
    23.  

    Instead of this:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public File messagesFile;
    5. public FileConfiguration messages;
    6.  
    7. @Override
    8. public void onEnable() {
    9. messagesFile = new File(this.getDataFolder() + "messages.yml");
    10. if (!getDataFolder().exists()) {
    11. getDataFolder().mkdirs();
    12. }
    13. if (!messagesFile.exists()) {
    14. this.saveResource("messages.yml", false);
    15. }
    16. messages = YamlConfiguration.loadConfiguration(messagesFile);
    17. new CommandWorld(this);
    18. }
    19. }
    20.  

    It seems it was the Main class that didn't load the file correctly so it always returned null at every startup. (Also i needed a / before the "messages.yml" because it should be inside the plugins folder) Thanks for all the help. :) Changing to solved now.
     
    Last edited by a moderator: May 3, 2018
Thread Status:
Not open for further replies.

Share This Page