-1

I see this error sometimes (sometimes it appears and sometimes not) when i "hit" in yellow point (image) but i have no idea why.. I know maybe its look very chaotic but it's my first time when i a make game in java.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 22
at Gameplay.paint(Gameplay.java:140)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

And here is my paint method:

 if (enemyXPos[xPos] == snakeXLenght[0] && enemyYPos[yPos] == snakeYLenght[0]) {

        score++;
        lenghtOfSnake++;
        xPos = random.nextInt(34);
        yPos = random.nextInt(23);
    }

    //line140:
    enemyImage.paintIcon(this, graphics, enemyXPos[xPos], enemyYPos[yPos]);


    for (int b = 1; b < lenghtOfSnake; b++) {
        if (snakeXLenght[b] == snakeXLenght[0] && snakeYLenght[b] == snakeYLenght[0]) {
            right = false;
            left = false;
            up = false;
            down = false;

            graphics.setColor(Color.white);
            graphics.setFont(new Font("arial", Font.BOLD, 50));
            graphics.drawString("YOU LOSE", 300, 300);

            graphics.setFont(new Font("arial", Font.BOLD, 20));
            graphics.drawString("Space to  RESTART", 350, 340);
        }
    }

    graphics.dispose();
}

and here are my arrays:

private int[] enemyXPos = {25, 50,  75,  100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850};
private int[] enemyYPos = {75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 575, 600, 625};

if i showed not enough code, just say and i paste more.

Ben Leggiero
  • 25,904
  • 38
  • 161
  • 267
Pablito
  • 11
  • 4
  • 1
    Which line is 140? Your problem is either the value of xPos, yPos, or b. – nicomp May 21 '18 at 18:30
  • "enemyImage.paintIcon(this, graphics, enemyXPos[xPos], enemyYPos[yPos]);" this is a line 140 – Pablito May 21 '18 at 18:32
  • 1
    Double check the arrays for enemy positioning. My guess would be that `xPos = random.nextInt(34); yPos = random.nextInt(23);` one of these are greater than the size of the array. – RJ7 May 21 '18 at 18:33
  • If I had to guess I'd say this is a simple off by one error. I agree with @RJ7 , check your arrays. – ethan codes May 21 '18 at 18:36
  • Have you tried to understand what the exception means and how it can help you troubleshoot your problem? – Joakim Danielson May 21 '18 at 18:39

2 Answers2

0

In Gameplay.java, on line 140 (in your paint() method), you are trying to get something out of an array. That array is smaller than the index you are giving it (you are trying to get item at index 22, but the array might have 21 or fewer items).

Make sure enemyXPos and enemyYPos are both equal to or larger than xPos and yPos, respectively.

Ben Leggiero
  • 25,904
  • 38
  • 161
  • 267
  • private int[] enemyXPos = {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850}; private int[] enemyYPos = {75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 575, 600, 625}; – Pablito May 21 '18 at 18:38
  • 1
    @Pablito Please [edit] your question instead of posting your update in the comments – GBlodgett May 21 '18 at 18:44
  • 1
    Sorry for that illegible post but you had right, my array "enemyYPos" have 22 elements and i wanted use 23 elements... :D but still i have no idea why only sometimes i had this error and not when i started the app – Pablito May 21 '18 at 18:58
  • Glad I could help. Best of luck, @Pablito! – Ben Leggiero May 21 '18 at 19:02
  • 1
    @Pablito, you only get this sometimes because the error depends on the value coming back from your `random.nextInt` call. – RJ7 May 22 '18 at 15:58
-2

This code is not enough to give a concrete answer, but it looks like your enemyXPos or enemyYPos sizes are not enough.

Amila
  • 4,977
  • 1
  • 24
  • 45
  • private int[] enemyXPos = {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850}; private int[] enemyYPos = {75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 575, 600, 625}; – Pablito May 21 '18 at 18:36
  • This should be a comment, for future reference. It is not a properly formatted answer. – ethan codes May 21 '18 at 18:37