NullPointerException

Discussion in 'Plugin Development' started by TheHandfish, May 2, 2014.

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

    TheHandfish

    Sorry for asking for help so much. >_> But you guys really are helping! :)

    So here's my problem. I'm advancing towards a placement code. It runs through, organizing all the players into their places. Depending on their place (1-8) it assigns them to a String[] called "Placement." Then I broadcast the placing. i.e. "First place " + Placement[1] It should work. However, I'm getting a NullPointerException at line 25. What did I do wrong? Thanks!

    Here is my code:

    Code:javascript
    1. public static String[] Placement;
    2.  
    3. public static int getPlayerPlacing(Player p)
    4. {
    5. int playerplace = 1;
    6. for(Player online : Bukkit.getServer().getOnlinePlayers())
    7. {
    8. if(p.getMetadata("playerScore").get(0).asInt() > online.getMetadata("playerScore").get(0).asInt() && !p.equals(online) && playerplace != 1)
    9. {
    10. playerplace--;
    11. }
    12. else if(p.getMetadata("playerScore").get(0).asInt() < online.getMetadata("playerScore").get(0).asInt() && playerplace != 8)
    13. {
    14. playerplace++;
    15. }
    16. }
    17. return playerplace;
    18. }
    19.  
    20. public static void placePlayers()
    21. {
    22.  
    23. for(int x = 1; x < 8; x = x+1)
    24. {
    25. Placement[x] = "--"; // <-- NULL POINTER EXCEPTION RIGHT HERE
    26. }
    27.  
    28. plugin.getServer().broadcastMessage("§3---------- §b§l CURRENT PLACING §3----------");
    29. for(Player online : Bukkit.getServer().getOnlinePlayers())
    30. {
    31. Placement[getPlayerPlacing(online)] = online.getDisplayName();
    32. }
    33. plugin.getServer().broadcastMessage("§e§oFirst place: §c§o" + Placement[1]);
    34. plugin.getServer().broadcastMessage("§7§oSecond place: §c§o" + Placement[2]);
    35. plugin.getServer().broadcastMessage("§6§oThird place: §c§o" + Placement[3]);
    36. plugin.getServer().broadcastMessage("§9§oFourth place: §c§o" + Placement[4]);
    37. plugin.getServer().broadcastMessage("§3§oFifth place: §c§o" + Placement[5]);
    38. plugin.getServer().broadcastMessage("§5§oSixth place: §c§o" + Placement[6]);
    39. plugin.getServer().broadcastMessage("§5§oSeventh place: §c§o" + Placement[7]);
    40. plugin.getServer().broadcastMessage("§5§oEight place: §c§o" + Placement[8]);
    41.  
    42. plugin.getServer().broadcastMessage("§3--------------------------------------------");
    43.  
    44. }
     
  2. Offline

    raGan.

    You need to define your array first. Like this
    Code:
    String[] placement = new String[8];
    Arrays also start from 0, not 1, so array of size 8 has indexes 0-7.

    You might want to go through tutorial or two to help you speed up your coding and avoid issues like this.
     
Thread Status:
Not open for further replies.

Share This Page