Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100

YoungStarDC

We are trying to create a game board with tiles (10 x 10). However, when running the class below, we keep getting an ArrayIndexOutOfBoundsException. For some reason, when running the same class on another device, this error is not given.

Board.java

public class Board extends JComponent {

    public Board() {

    }


    public static String[] gameElements = new String[100];


    String[][] Map = new String[10][10];
    int positionX = 50;
    int positionY = 50;
    int i = 0;
    String currentLevel = "1";

    @Override
    public void paintComponent(Graphics g) {
        loadLevel();
        for (int y = 0; y < Map.length; y++) {
            for (int x = 0; x < Map.length; x++) {
                new Tile(x, y).paintComponent(g);
                Map[y][x] = gameElements[i];
                g.drawString(Map[y][x], positionY, positionX);
                positionY = positionY + 50;
                System.out.print("[" + Map[y][x] + "]");
                i++;

            }
            positionY = 50;
            positionX = positionX + 50;
            System.out.println();

        }
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setSize(600, 600);
        frame.setTitle("SleutelBarricade");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent chart = new Board();
        frame.add(chart);

        frame.setVisible(true);


    }

    public void readTextFile(String fileName) {
        try {
            FileReader fileReader = new FileReader(fileName + ".txt");
            BufferedReader buffer = new BufferedReader(fileReader);
            String splitBy = ",";
            String line = buffer.readLine();

            for (int i = 0; i < gameElements.length; i++) {
                gameElements = line.split(splitBy);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadLevel() {
        readTextFile(currentLevel);

    }

}

Part of the Exception:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100
    at Main.GameBoard.Board.paintComponent(Board.java:45)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
Mr. Polywhirl

You never reset i before your double-nested loop begins. Move the variable inside or reset it to 0.

@Override
public void paintComponent(Graphics g) {
    int i = 0;
    loadLevel();

    for (int y = 0; y < Map.length; y++) {
        for (int x = 0; x < Map.length; x++) {
            new Tile(x, y).paintComponent(g);
            Map[y][x] = gameElements[i];
            g.drawString(Map[y][x], positionY, positionX);
            positionY = positionY + 50;
            System.out.print("[" + Map[y][x] + "]");
            i++;

        }

        positionY = 50;
        positionX = positionX + 50;
        System.out.println();
    }
}

I would also suggest you reorganize your class to make it easier to read. You do not have to follow this to the letter, but try to group similar things. This will make things stand out better and help others follow your program more easily.

With this simple tip, the stray int i = 0 (instance variable) would have been easy to spot.

public class Board extends JComponent {
    // I. Static variables
    public static String[] gameElements = new String[100];

    // II. Instance variables (These should be private or protected)
    private String[][] map = new String[10][10];
    private int positionX = 50;
    private int positionY = 50;
    private int i = 0; // <------------------------- Hey, you don't belong here!
    private String currentLevel = "1";

    // III. Getter/Setters
    public String[] getMap() {
        return map;
    }
    public void setMap(String[] map) {
        this.map = map;
    }

    // IV. Constructor
    public Board() { }

    // V. Overrides
    @Override
    public void paintComponent(Graphics g) { }

    // VI. Custom Instance Methods
    public void readTextFile(String fileName) { }

    public void loadLevel() { }

    // VII. Main Method
    public static void main(String[] args) { }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in basic Java GUI interest calculator

From Dev

java parseint - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

From Dev

What is this: Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError?

From Dev

Matlab reports "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException [NetBeans]

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException if-statement

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "11101110110100011110111011010001"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Login form error

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException For Loops

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Panel displays before fully loaded?)

From Dev

NetBeans Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: DSA

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 8

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and JTable problems

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in GUI?

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "FALSE"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and how to fix it?

From Dev

Exception in "AWT-EventQueue-0" java.lang.NullPointerException

From Dev

Exception in "AWT-EventQueue-0" java.lang.NullPointerException

From Dev

Java error in my code Exception in thread "AWT-EventQueue-0"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method)

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.put(Ljava/lang/Object;)V

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to university.pojo.Schedule

From Dev

Jframe Error: Exception: "AWT-EventQueue-0" java.lang.NullPointerException

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel

From Dev

Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

From Dev

Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

Related Related

  1. 1

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException

  2. 2

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in basic Java GUI interest calculator

  3. 3

    java parseint - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

  4. 4

    What is this: Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError?

  5. 5

    Matlab reports "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

  6. 6

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException [NetBeans]

  7. 7

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException if-statement

  8. 8

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton

  9. 9

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "11101110110100011110111011010001"

  10. 10

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Login form error

  11. 11

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException For Loops

  12. 12

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract

  13. 13

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Panel displays before fully loaded?)

  14. 14

    NetBeans Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: DSA

  15. 15

    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 8

  16. 16

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and JTable problems

  17. 17

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in GUI?

  18. 18

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "FALSE"

  19. 19

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and how to fix it?

  20. 20

    Exception in "AWT-EventQueue-0" java.lang.NullPointerException

  21. 21

    Exception in "AWT-EventQueue-0" java.lang.NullPointerException

  22. 22

    Java error in my code Exception in thread "AWT-EventQueue-0"

  23. 23

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method)

  24. 24

    Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.put(Ljava/lang/Object;)V

  25. 25

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to university.pojo.Schedule

  26. 26

    Jframe Error: Exception: "AWT-EventQueue-0" java.lang.NullPointerException

  27. 27

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel

  28. 28

    Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

  29. 29

    Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

HotTag

Archive