Solved NullPointerException error

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

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

    KarimAKL

    I've got this error when i send messages from my messages.yml file. :/ Here is the code that gets the NullPointerException error:
    Code:Java
    1.  
    2. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("message").replace("{world}", world)));
    3.  

    And here is the "message" in the messages.yml file:
    Code:
    message: "&aYou are currently in world {world}"
    
    I think the problem is how i create the message.yml file in my main class, this is how i do it:
    Code:Java
    1.  
    2. public File messagesFile = new File(this.getDataFolder() + "messages.yml");
    3. public FileConfiguration messages = YamlConfiguration.loadConfiguration(messagesFile);
    4.  
    5. @Override
    6. public void onEnable() {
    7. if (!getDataFolder().exists()) {
    8. getDataFolder().mkdirs();
    9. if (!messagesFile.exists()) {
    10. this.saveResource("messages.yml", false);
    11. }
    12. } else {
    13. if (!messagesFile.exists()) {
    14. this.saveResource("messages.yml", false);
    15. }
    16.  

    And here is the error when i try executing the command:
    Code:
    [09:04:53 INFO]: Karim09 issued server command: /world
    [09:04:53 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'world' in plugin WorldTeller v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at me.karim.worldteller.commands.CommandWorld.onCommand(CommandWorld.java:62) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 15 more
    
    I have tried changing the line which causes the error to something like this:
    Code:Java
    1.  
    2. p.sendMessage("Does this work?");
    3.  

    And then it didn't cause any errors. :7 Btw the "ChatColorUtil.chat" is just a static for ChatColor.translateAlternateColorCodes :p
     
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Please post the involved classes in full.
    My first guess is that plugin is null.
     
  3. Offline

    KarimAKL

    @timtower What do you mean "plugin is null"?
     
  4. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Like I said, you have a plugin variable, it can be null.
     
  5. Offline

    KarimAKL

    @timtower Sorry but i don't know what a "plugin variable" is. :/
     
  6. Online

    timtower Administrator Administrator Moderator

    @KarimAKL
    plugin.messages.getString(
    One that you made by the looks of it.
     
  7. Offline

    KarimAKL

    @timtower This?:
    Code:Java
    1.  
    2. public class CommandWorld implements CommandExecutor {
    3.  
    4. private Main plugin;
    5.  
    6. public CommandWorld(Main plugin) {
    7. this.plugin = plugin;
    8. plugin.getCommand("world").setExecutor(this);
    9. }
    10.  
     
  8. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Yes, that one, now post your full onEnable and full CommandWorld class please
     
  9. Offline

    KarimAKL

    @timtower This is my full onEnable:
    Code:Java
    1.  
    2. @Override
    3. public void onEnable() {
    4. if (!getDataFolder().exists()) {
    5. getDataFolder().mkdirs();
    6. if (!messagesFile.exists()) {
    7. this.saveResource("messages.yml", false);
    8. }
    9. new CommandWorld(this);
    10. } else {
    11. if (!messagesFile.exists()) {
    12. this.saveResource("messages.yml", false);
    13. }
    14. new CommandWorld(this);
    15. }
    16. }
    17.  

    And this is my full CommandWorld class:
    Code:Java
    1.  
    2. public class CommandWorld implements CommandExecutor {
    3.  
    4. private Main plugin;
    5.  
    6. public CommandWorld(Main plugin) {
    7. this.plugin = plugin;
    8. plugin.getCommand("world").setExecutor(this);
    9. }
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    13. if (cmd.getName().equalsIgnoreCase("world")) {
    14. if (!(sender instanceof Player)) {
    15. sender.sendMessage(plugin.messages.getString("World.player-only"));
    16. return true;
    17. } else {
    18. Player p = (Player) sender;
    19. String world = p.getWorld().getName();
    20. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("World.message").replace("{world}", world)));
    21. }
    22. }
    23. return false;
    24. }
    25.  
    26. }
    27.  
     
  10. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Then messages is null.
    I believe that we said to load the configs in the onEnable, not before that.
    And you have nested things that have no need to be nested.
     
  11. Offline

    KarimAKL

    @timtower Like this?:
    Code:Java
    1.  
    2. @Override
    3. public void onEnable() {
    4. if (!getDataFolder().exists()) {
    5. getDataFolder().mkdirs();
    6. if (!messagesFile.exists()) {
    7. this.saveResource("messages.yml", false);
    8. try {
    9. this.messages.save(messagesFile);
    10. this.messages.load(messagesFile);
    11. } catch (IOException | InvalidConfigurationException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. new CommandWorld(this);
    16. } else {
    17. if (!messagesFile.exists()) {
    18. this.saveResource("messages.yml", false);
    19. try {
    20. this.messages.save(messagesFile);
    21. this.messages.load(messagesFile);
    22. } catch (IOException | InvalidConfigurationException e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. new CommandWorld(this);
    27. }
    28. }
    29.  

    I just tried it and it still comes with the same error, so it's probably not like that. :7 Also, what does "nested" mean?
     
  12. Online

    timtower Administrator Administrator Moderator

    @KarimAKL And now without the nested mess.
    Because when the file does exist then you never load anything.

    And nested is having { in a { in a {
     
  13. Offline

    KarimAKL

    @timtower Like this?:
    Code:Java
    1.  
    2. @Override
    3. public void onEnable() {
    4. if (!getDataFolder().exists()) {
    5. getDataFolder().mkdirs();
    6. if (!messagesFile.exists()) {
    7. this.saveResource("messages.yml", false);
    8. }
    9. try {
    10. this.messages.save(messagesFile);
    11. this.messages.load(messagesFile);
    12. } catch (IOException | InvalidConfigurationException e) {
    13. e.printStackTrace();
    14. }
    15. new CommandWorld(this);
    16. } else {
    17. if (!messagesFile.exists()) {
    18. this.saveResource("messages.yml", false);
    19. }
    20. try {
    21. this.messages.save(messagesFile);
    22. this.messages.load(messagesFile);
    23. } catch (IOException | InvalidConfigurationException e) {
    24. e.printStackTrace();
    25. }
    26. new CommandWorld(this);
    27. }
    28. }
    29.  

    EDIT: What about this?:
    Code:Java
    1.  
    2. @Override
    3. public void onEnable() {
    4. if (!getDataFolder().exists()) {
    5. getDataFolder().mkdirs();
    6. }
    7. if (!messagesFile.exists()) {
    8. this.saveResource("messages.yml", false);
    9. }
    10. try {
    11. this.messages.save(messagesFile);
    12. this.messages.load(messagesFile);
    13. } catch (IOException | InvalidConfigurationException e) {
    14. e.printStackTrace();
    15. }
    16. new CommandWorld(this);
    17. }
    18.  
     
  14. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You don't need to save messages, and you are still missing the initialization of messagesFile in the onEnable.
     
  15. Offline

    KarimAKL

    @timtower Okay so i remove the "this.messages.save" and then how would i add the initialization of messagesFile?
     
  16. Online

    timtower Administrator Administrator Moderator

  17. Offline

    KarimAKL

    @timtower I need to put this:
    Code:Java
    1.  
    2. public File messagesFile = new File(this.getDataFolder() + "messages.yml");
    3. public FileConfiguration messages = YamlConfiguration.loadConfiguration(messagesFile);
    4.  

    into the onEnable then?
     
  18. Online

    timtower Administrator Administrator Moderator

  19. Offline

    KarimAKL

    @timtower You mean like this?:
    Code:Java
    1.  
    2. public File messagesFile;
    3. public FileConfiguration messages;
    4.  
    5. @Override
    6. public void onEnable() {
    7. messagesFile = new File(this.getDataFolder() + "messages.yml");
    8. messages = YamlConfiguration.loadConfiguration(messagesFile);
    9. if (!getDataFolder().exists()) {
    10. getDataFolder().mkdirs();
    11. }
    12. if (!messagesFile.exists()) {
    13. this.saveResource("messages.yml", false);
    14. }
    15. try {
    16. this.messages.load(messagesFile);
    17. } catch (IOException | InvalidConfigurationException e) {
    18. e.printStackTrace();
    19. }
    20. new CommandWorld(this);
    21. }
    22.  
     
  20. Online

    timtower Administrator Administrator Moderator

    @KarimAKL
    messages = YamlConfiguration.loadConfiguration(messagesFile);
    That line needs to be after the saveResource, and then you can also remove the
    try {
    this.messages.load(messagesFile);
    } catch (IOException | InvalidConfigurationException e) {
    e.printStackTrace();
    }
     
  21. Offline

    KarimAKL

    @timtower Okay, will try to do that now. :)
    EDIT: Still comes with the same error. :/
    Here is the code:
    Code:Java
    1.  
    2. public File messagesFile;
    3. public FileConfiguration messages;
    4.  
    5. @Override
    6. public void onEnable() {
    7. messagesFile = new File(this.getDataFolder() + "messages.yml");
    8. if (!getDataFolder().exists()) {
    9. getDataFolder().mkdirs();
    10. }
    11. if (!messagesFile.exists()) {
    12. this.saveResource("messages.yml", false);
    13. messages = YamlConfiguration.loadConfiguration(messagesFile);
    14. }
    15. new CommandWorld(this);
    16. }
    17.  
     
    Last edited by a moderator: May 3, 2018
  22. Online

    timtower Administrator Administrator Moderator

    @KarimAKL And why is the loadConfiguration in an if statement?
     
  23. Offline

    KarimAKL

    @timtower Whoops. :p Like this?:
    Code:Java
    1.  
    2. public File messagesFile;
    3. public FileConfiguration messages;
    4.  
    5. @Override
    6. public void onEnable() {
    7. messagesFile = new File(this.getDataFolder() + "messages.yml");
    8. if (!getDataFolder().exists()) {
    9. getDataFolder().mkdirs();
    10. }
    11. if (!messagesFile.exists()) {
    12. this.saveResource("messages.yml", false);
    13. }
    14. messages = YamlConfiguration.loadConfiguration(messagesFile);
    15. new CommandWorld(this);
    16. }
    17.  

    EDIT: I just tried that and i still comes with the error, either i did something wrong or i still need to do something. :/
     
  24. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Then find out what is null, it is plugin? It is messages? Is it the string itself?
     
  25. Offline

    KarimAKL

    @timtower I don't know. :/
    Error:
    Code:
    [10:58:55 INFO]: Karim09 issued server command: /world
    [10:58:55 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'world' in plugin WorldTeller v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at me.karim.worldteller.commands.CommandWorld.onCommand(CommandWorld.java:29) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 15 more
    
     
  26. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Then find out!
    Add print statements!
    The log won't tell you everything.
     
  27. Offline

    KarimAKL

    @timtower I see, will try to put some messages in between then.
    EDIT: I just tried doing this:
    Code:Java
    1.  
    2. public File messagesFile;
    3. public FileConfiguration messages;
    4.  
    5. @Override
    6. public void onEnable() {
    7. messagesFile = new File(this.getDataFolder() + "messages.yml");
    8. if (!getDataFolder().exists()) {
    9. getDataFolder().mkdirs();
    10. Bukkit.getConsoleSender().sendMessage(ChatColorUtil.chat("Main onEnable mkdirs"));
    11. }
    12. if (!messagesFile.exists()) {
    13. this.saveResource("messages.yml", false);
    14. Bukkit.getConsoleSender().sendMessage(ChatColorUtil.chat("Main onEnable saveResource"));
    15. }
    16. messages = YamlConfiguration.loadConfiguration(messagesFile);
    17. new CommandWorld(this);
    18. Bukkit.getConsoleSender().sendMessage(ChatColorUtil.chat("Main onEnable"));
    19. }
    20.  

    But the messages didn't get send to the console, what would that mean? Maybe that the plugin didn't start up correctly or something?
     
    Last edited by a moderator: May 3, 2018
  28. Online

    timtower Administrator Administrator Moderator

    @KarimAKL
    Code:
    public File messagesFile;
    public FileConfiguration messages;
    @Override
    public void onEnable() {
       System.out.println("onEnable 1");
      messagesFile = new File(this.getDataFolder() + "messages.yml");
       System.out.println("onEnable 2");
      if (!getDataFolder().exists()) {
         System.out.println("onEnable 3");
      getDataFolder().mkdirs();
         System.out.println("onEnable 4");
      }
       System.out.println("onEnable 5");
      if (!messagesFile.exists()) {
         System.out.println("onEnable 5");
      this.saveResource("messages.yml", false);
         System.out.println("onEnable 6");
      }
       System.out.println("onEnable 7");
      messages = YamlConfiguration.loadConfiguration(messagesFile);
       System.out.println("onEnable 8");
      new CommandWorld(this);
       System.out.println("onEnable 9");
    }
    Try that one, indentation is messed up because you are using spaces and I am using tabs.
     
  29. Offline

    KarimAKL

    @timtower I just tried using the "System.out.println("onEnable 1")" etc and all of them got send in the console, so what now?
     
  30. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Then you print the values of the things you use, so messages and the string inside messages
     
Thread Status:
Not open for further replies.

Share This Page