java.lang.IllegalArgumentException: Name cannot be null

Discussion in 'Plugin Development' started by TehVoyager, Nov 4, 2013.

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

    TehVoyager

    I'm getting this error:
    Code:
    java.lang.IllegalArgumentException: Name cannot be null
    from this class when calling the start game method
    Code:java
    1. package mcadventure.hunt;
    2.  
    3. import java.awt.List;
    4. import java.util.HashMap;
    5. import java.util.Random;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class HuntArena {
    12.  
    13. HashMap<Integer, String> playing = new HashMap<Integer, String>();
    14.  
    15. public enum Team{
    16. Hunt, Prey
    17. }
    18.  
    19. public enum GameState{
    20. Lobby, FirstFive, Hunt, EndGame
    21. }
    22.  
    23. public HashMap<String, Team> aliveplayers = new HashMap<String, Team>();
    24. int number = 0;
    25. GameState gs = GameState.Lobby;
    26. boolean pvp;
    27. int id = 1;
    28. HuntMessenger message = new HuntMessenger();
    29.  
    30. public void setGameState(GameState game){
    31. gs = game;
    32. }
    33.  
    34. public GameState getGameState(){
    35. return gs;
    36. }
    37.  
    38. public int getId(){
    39. return id;
    40. }
    41.  
    42. public void startGame(){
    43. gs = GameState.FirstFive;
    44. if(aliveplayers.size() < 0){
    45. for(Player plr : Bukkit.getServer().getOnlinePlayers()){
    46. plr.kickPlayer(ChatColor.RED + "Not enough prey D:");
    47. }
    48. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "reload");
    49. }
    50. Random ra = new Random();
    51. List hunterlist = new List();
    52. int hunters = (playing.size() + 1) / 5 + 1;
    53. try{while(hunters != 0){
    54. String plr = Bukkit.getServer().getPlayer(playing.get(ra.nextInt())).getName();
    55. Bukkit.broadcastMessage(plr);
    56. hunters--;
    57. }
    58. message.BroadCast("The Game Has Begun, The Hunters are " + hunterlist);
    59. }catch(Exception e){
    60. Bukkit.broadcastMessage(e.toString());
    61. }
    62. }
    63. public void addPlaying(String name){
    64. playing.put(number, name);
    65. }
    66.  
    67. public void addPlayer(String str, Team t){
    68. aliveplayers.remove(str);
    69. aliveplayers.put(str, t);
    70. }
    71.  
    72. public void removePlayer(String str){
    73. aliveplayers.remove(str);
    74. }
    75.  
    76. public Team getTeam(String s){
    77. return aliveplayers.get(s);
    78. }
    79.  
    80. public boolean getPvP(){
    81. return pvp;
    82. }
    83. public void setPvP(Boolean b){
    84. pvp = b;
    85. }
    86. }


    I really don't understand the error or whats causing it
     
  2. Offline

    adam753

    Don't do this:
    Code:
    catch(Exception e){
        Bukkit.broadcastMessage(e.toString());
    }
    Do this instead:
    Code:
    catch(Exception e){
        e.printStackTrace();
    }
    It will print out a more detailed error message,mainly including the line the error is coming from. But since that already narrows it down to 3 possible lines (inside the try block), it's probably this one:
    Code:java
    1.  
    2. String plr = Bukkit.getServer().getPlayer(playing.get(ra.nextInt())).getName();
    3.  

    There's no guarantee that random.nextInt() will be below the size of the list. In fact, its upper limit is 2,147,483,647. However, you can set the value's upper limit yourself like this:
    Code:java
    1.  
    2. ra.nextInt(5)
    3. //Random number between 0 and 4 (inclusive)
    4.  
    5. ra.nextInt(playing.size())
    6. //Random number within the size of the list
    7.  
     
  3. Offline

    TehVoyager

    adam753
    Oh yeah forgot to set thwe nextint max to player total xD
     
Thread Status:
Not open for further replies.

Share This Page