Solved Very strange UnsupportedOperationException on ArrayList - Can't figure it out ...

Discussion in 'Plugin Development' started by woutwoot, Jan 8, 2014.

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

    woutwoot

    So, this is my code. It's a simple plugin that should just count my name in private chat and public chat:
    WoutCount class (I left out some code, full source at the end of the thread):
    Code:java
    1. public class WoutCount extends JavaPlugin implements Listener, CommandExecutor {
    2.  
    3. private List<Score> highscores = new ArrayList<Score>();
    4.  
    5. //Enable, disable and commandlistener here
    6.  
    7. @EventHandler
    8. public void onCommandPreprocess(PlayerCommandPreprocessEvent event) {
    9. if((event.getMessage().toLowerCase().contains("r") || event.getMessage().toLowerCase().contains("msg")) && event.getMessage().toLowerCase().contains("wout")){
    10. reCalculateScores(event.getPlayer());
    11. }
    12. }
    13.  
    14. @EventHandler
    15. public void onPlayerChat(final AsyncPlayerChatEvent event) {
    16. if(event.getMessage().toLowerCase().startsWith("wout") || event.getMessage().toLowerCase().contains("wout")){
    17. reCalculateScores(event.getPlayer());
    18. }
    19. }
    20.  
    21. private void saveHighScores(){
    22. //save code, works fine
    23. }
    24.  
    25. private void loadHighScores(){
    26. // load code, works fine
    27. }
    28.  
    29. private void reCalculateScores(Player p) {
    30. boolean found = false;
    31. for (int i = 0; i < highscores.size() && !found; i++) {
    32. Score h = highscores.get(i);
    33. if (h.getPlayername().equals(p.getName())) {
    34. h.setScore(h.getScore() + 1);
    35. found = true;
    36. }
    37. }
    38.  
    39. if (!found) {
    40. Score h = new Score(p, 1);
    41. try {
    42. highscores.add(h);
    43. this.getLogger().log(Level.WARNING, "The exception is there again ...");
    44. }
    45. }
    46.  
    47. Score[] scores = highscores.toArray(new Score[highscores.size()]);
    48. scores = Tools.selectSortScores(scores);
    49. highscores = Arrays.asList(scores);
    50. saveHighScores();
    51. }
    52.  
    53. }


    Sort from tools class:
    Code:java
    1. public static Score[] selectSortScores(Score[] arr) {
    2. for (int i = 0; i < arr.length - 1; i++) {
    3. int iG = i;
    4. for (int j = i + 1; j < arr.length; j++) {
    5. if (arr[j].getScore() > arr[iG].getScore()) iG = j;
    6. }
    7. Score tmp = arr[I];[/I]
    8. [I] arr[I] = arr[iG];[/I][/I]
    9. [I] arr[iG] = tmp;[/I]
    10. [I] }[/I]
    11. [I] return arr;[/I]
    12. [I] }[/I]


    highscores.add(h); Keeps throwing the exception, however, for someone that is already in the list it does work. I have no Idea what this problem is.
     
  2. Offline

    Garris0n

    A google search returns that the list returned by this method does not work with the .add() method as it has a fixed size. Try something like highscores.addAll(Arrays.asList(scores));
     
    woutwoot likes this.
  3. Offline

    woutwoot

    You sir, are awesome! THANKS!
     
Thread Status:
Not open for further replies.

Share This Page