Solved NullPointerException error

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

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

    KarimAKL

    @timtower So pretty much i use the "System.out.println()" on everything my CommandWorld class?
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    KarimAKL

    @timtower I do use tab but when writing here it usually gets a little weird when copying so i use space here, anyway, when doing this:
    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. sender.sendMessage(plugin.messages.getString("World.player-only"));
    9. System.out.println("player-only message");
    10. return true;
    11. } else {
    12. System.out.println("else (sender is player)");
    13. Player p = (Player) sender;
    14. String world = p.getWorld().getName();
    15. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("World.message").replace("{world}", world)));
    16. System.out.println("World message");
    17. return true;
    18. }
    19. }
    20. return false;
    21. }
    22.  

    And then executing the command as console the only messages that showed up was the "cmd.getName" and "!sender instanceof Player" (not the "player-only message" aswell) and when executing as a player it only showed the messages "cmd.getName" and "else (sender is player)" (not "World message") so what does that mean?
    EDIT: Of course the error aswell but of what i wrote.
     
  4. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Print this one: plugin.messages.getString("World.player-only")
    I think that your yml is empty, or at least doesn't contain that one.
     
  5. Offline

    KarimAKL

    @timtower Here is my messages.yml:
    Code:Java
    1.  
    2. World:
    3. player-only: "&cOnly players may execute this command!"
    4. message: "&aYou are currently in world {world}"
    5.  
     
  6. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Is that the one in the plugin jar or the one in the plugins folder?
     
  7. Offline

    KarimAKL

    @timtower Well i copy and pasted that from eclipse(the jar) but when i look at it in the plugins folder it's exactly the same. :/
    EDIT: Just so you can see:
    Code:Java
    1.  
    2. World:
    3. player-only: "&cOnly players may execute this command!"
    4. message: "&aYou are currently in world {world}"
    5.  

    That is copy and pasted from the file in the plugins folder.
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    KarimAKL

    @timtower My full server log? Where do i find it?
    EDIT: Or do you mean all the text when the server is starting?
     
  10. Online

    timtower Administrator Administrator Moderator

    @KarimAKL <server directory>/logs/latest.log
     
  11. Offline

    KarimAKL

  12. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Can you post your updated onCommand?
     
  13. Offline

    KarimAKL

    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. sender.sendMessage(plugin.messages.getString("World.player-only"));
    9. System.out.println("player-only message");
    10. return true;
    11. } else {
    12. System.out.println("else (sender is player)");
    13. Player p = (Player) sender;
    14. String world = p.getWorld().getName();
    15. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("World.message").replace("{world}", world)));
    16. System.out.println("World message");
    17. return true;
    18. }
    19. }
    20. return false;
    21. }
    22.  

    This is the current onCommand.
     
  14. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Again, print
    plugin.messages.getString("World.player-only")
     
  15. Offline

    KarimAKL

    @timtower What do you mean? I have this:
    Code:Java
    1.  
    2. sender.sendMessage(plugin.messages.getString("World.player-only"));
    3.  

    Also what do you mean by print it? Like this one?:
    Code:Java
    1.  
    2. System.out.println("player-only message");
    3.  
     
  16. Online

    timtower Administrator Administrator Moderator

    @KarimAKL System.out.println(plugin.messages.getString("World.player-only"))
    If that is throwing errors:
    System.out.println(plugin);
    System.out.println(plugin.messages);
     
  17. Offline

    KarimAKL

    @timtower It doesn't print the message when i run the command. :/
     
  18. Online

    timtower Administrator Administrator Moderator

    New onCommand?
     
  19. Offline

    KarimAKL

    @timtower
    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. System.out.println(plugin.messages.getString("World.player-only"));
    9. return true;
    10. } else {
    11. System.out.println("else (sender is player)");
    12. Player p = (Player) sender;
    13. String world = p.getWorld().getName();
    14. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("World.message").replace("{world}", world)));
    15. System.out.println("World message");
    16. return true;
    17. }
    18. }
    19. return false;
    20. }
    21.  
     
  20. Online

    timtower Administrator Administrator Moderator

    @KarimAKL And that doesn't print? Does it throw an error?
     
  21. Offline

    KarimAKL

  22. Online

    timtower Administrator Administrator Moderator

    Then it is not running at all. Something you need to debug and fix.
     
  23. Offline

    KarimAKL

    @timtower Any idea how i would make it run and what is wrong?
    Current onCommand:
    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. System.out.println(plugin.messages.getString("World.player-only"));
    9. return true;
    10. } else {
    11. System.out.println("else (sender is player)");
    12. Player p = (Player) sender;
    13. String world = p.getWorld().getName();
    14. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("World.message").replace("{world}", world)));
    15. System.out.println("World message");
    16. return true;
    17. }
    18. }
    19. return false;
    20. }
    21.  

    This does run when i just write a message myself instead of using a yml file.
     
  24. Online

    timtower Administrator Administrator Moderator

    @KarimAKL On which case are you focusing? The one where the sender is a player, or the one where the sender is the console?
     
  25. Offline

    KarimAKL

  26. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Which command are you testing? The one for the player or the one for the console?
     
  27. Offline

    KarimAKL

    @timtower I'm testing both of them, i try the command on both the console and as a player on my server.
     
  28. Online

    timtower Administrator Administrator Moderator

    Lets just use the console version for now.
    What happens if you run the command?
     
  29. Offline

    KarimAKL

    @timtower As the console: It sends "cmd.getName" and then "!sender instanceof Player" and then it comes with the error.
     
  30. Online

    timtower Administrator Administrator Moderator

     
Thread Status:
Not open for further replies.

Share This Page