StackOverflowError

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • StackOverflowError

    Hallo!

    Ich habe mit folgenden Fehler zu kämpfen, wenn ich die Klasse aufrufe mit

    Quellcode

    1. CRider ride = new CRider(jPanel1);


    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.misc.Unsafe.compareAndSwapLong(Native Method)
    at java.util.concurrent.atomic.AtomicLong.compareAndSet(AtomicLong.java:121)
    at java.util.Random.next(Random.java:139)
    at java.util.Random.nextInt(Random.java:253)
    at PRider.CRider.paintStep(CRider.java:65)
    at PRider.CRider.paintStep(CRider.java:86)
    at PRider.CRider.paintStep(CRider.java:76)
    at PRider.CRider.paintStep(CRider.java:96)
    usw...


    Quellcode

    1. /*
    2. * CRider.java
    3. *
    4. * Created on 20. März 2007
    5. *
    6. * To change this template, choose Tools | Template Manager
    7. * and open the template in the editor.
    8. */
    9. package PRider;
    10. import java.awt.*;
    11. import java.util.*;
    12. import javax.swing.JPanel;
    13. /**
    14. *
    15. * @author Max123
    16. */
    17. public class CRider {
    18. int x;
    19. int y;
    20. Graphics g;
    21. boolean alive;
    22. int lastx;
    23. int lasty;
    24. boolean matrix[][];
    25. Random r;
    26. int richtung = 0;
    27. /** Creates a new instance of CRider */
    28. public CRider(JPanel area) {
    29. r = new Random();
    30. alive = true;
    31. g = area.getGraphics();
    32. x = area.WIDTH;
    33. y = area.HEIGHT;
    34. matrix = new boolean[x+2][y+2];
    35. for (int i=0; i<x+2; i++) {
    36. matrix[i][0] = false;
    37. matrix[i][y+1] = false;
    38. }
    39. for (int i=0; i<y+2; i++) {
    40. matrix[0][i] = false;
    41. matrix[x+1][i] = false;
    42. }
    43. lastx = r.nextInt(x)+1;
    44. lasty = r.nextInt(y)+1;
    45. while (alive)
    46. paintStep(0);
    47. }
    48. private void paintStep(int tiefe) {
    49. if (alive) {
    50. if(richtung == 0) {
    51. if(!(lastx+1 > x) || matrix[lastx+1][lasty]) {
    52. lastx += 1;
    53. } else {
    54. int old = richtung;
    55. while (old == richtung)
    56. richtung = r.nextInt(4);
    57. paintStep(tiefe+1);
    58. }
    59. }
    60. else if(richtung == 1) {
    61. if(!(lasty+1 > y) || matrix[lastx][lasty+1]) {
    62. lasty += 1;
    63. } else {
    64. int old = richtung;
    65. while (old == richtung)
    66. richtung = r.nextInt(4);
    67. paintStep(tiefe+1);
    68. }
    69. }
    70. else if(richtung == 2) {
    71. if(!(lastx-1 < x) || matrix[lastx-1][lasty]) {
    72. lastx -= 1;
    73. } else {
    74. int old = richtung;
    75. while (old == richtung)
    76. richtung = r.nextInt(4);
    77. paintStep(tiefe+1);
    78. }
    79. }
    80. else if(richtung == 3) {
    81. if(!(lasty-1 < y) || matrix[lastx][lasty-1]) {
    82. lasty -=1;
    83. } else {
    84. int old = richtung;
    85. while (old == richtung)
    86. richtung = r.nextInt(4);
    87. paintStep(tiefe+1);
    88. }
    89. }
    90. }
    91. g.drawRect(lastx-1,lasty-1,1,1);
    92. if (tiefe > 4)
    93. alive = false;
    94. }
    95. }
    Alles anzeigen


    Vorher bin ich bei den 4 ifs immer über die Arraygrenzen geschossen. Problem gelöst und jetzt das... xD

    Hat wer ne Tipp? ^^

    Gruß,
    Max123