Infinite for loop??

Discussion in 'Plugin Development' started by 1Camer0471, Aug 31, 2015.

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

    1Camer0471

    So when this code is executed:

    Code:Java
    1.  
    2. public void updatePage(int i) {
    3.  
    4. Inventory page = this.getAH().get(i);
    5.  
    6. System.out.println(page.getContents().length);
    7.  
    8. if (!this.getItems().isEmpty()) {
    9. for (int index = 45; index <= 45 && index > -1; index--) {
    10. if (i < this.getAH().size()) {
    11. System.out.println("a");
    12. if (index + 1 <= 45) {
    13. System.out.println("b");
    14. if (index > -1) {
    15. System.out.println("c");
    16. if (this.getItems().contains(page.getItem(index))) {
    17. System.out.println("d");
    18. if (page.getItem(index++) == null
    19. || page.getItem(index++).getType() == Material.AIR) {
    20. System.out.println(index
    21. + ", "
    22. + page.getItem(index).getType()
    23. .name());
    24. page.setItem(index + 1, page.getItem(index));
    25. page.clear(index);
    26. }
    27. } else {
    28. page.clear(index);
    29. }
    30. }
    31. } else {
    32. if (page.getItem(index) != null
    33. && page.getItem(index).getType() != Material.AIR) {
    34. this.getAH().get(i++).addItem(page.getItem(index));
    35. }
    36. }
    37.  


    the console is spammed with
    a
    b
    a
    b
    a
    b
    until the server crashes.
     
    Last edited: Aug 31, 2015
  2. Offline

    mythbusterma

    @1Camer0471

    So you start at forty-five and decrease one until it's less than or equal to forty-five.

    Think about your code before you write it.
     
    TheGamesHawk2001 and 1Camer0471 like this.
  3. Whatever the use of it, you check if the index (45) is smaller than or equal to 45, then decrement. Then it is still smaller and the loop goes on
     
    1Camer0471 likes this.
  4. Offline

    1Camer0471

    @FisheyLP @mythbusterma Thanks guys! That fixed the initial problem but now it spams a b c d then crashes, and I have no more loops running?
     
  5. Offline

    mythbusterma

    @1Camer0471

    You probably need to rewrite it, that code is quite the mess.

    If I had to guess, you're modifying the loop index inside the loop and causing it to loop infinitely.
     
    1Camer0471 likes this.
  6. Offline

    1Camer0471

    @mythbusterma for some reason it's not decreasing index by 1
     
  7. Offline

    mythbusterma

    @1Camer0471

    Probably because I can see two separate places where you're incrementing it.
     
Thread Status:
Not open for further replies.

Share This Page