Program won’t run after a while, and I get this

Tags: ,



I have really messed with this one for a while now, and I can’t figure it out.

I’m in the process of making an breakout game, but I keep getting this error suddenly:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at com.spil.Start.sortTable(Start.java:388)
at com.spil.Start.printScores(Start.java:440)
at com.spil.Start.paintComponent(Start.java:304)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Here’s the code for: at com.spil.Start.sortTable(Start.java:388)

public void sortTable() throws IOException {
    File f = new File("HighScores.txt");
    File temp = new File("temp.txt");
    TreeMap<Integer, ArrayList<String>> topTen = new TreeMap<Integer, ArrayList<String>>();
    BufferedReader br = new BufferedReader(new FileReader(f.getAbsoluteFile()));
    BufferedWriter bw = new BufferedWriter(new FileWriter(temp.getAbsoluteFile()));


    String line = null;
    while ((line = br.readLine()) != null) {
        if (line.isEmpty()) {
            continue;
        }
        String[] scores = line.split("Points: ");
        Integer score = Integer.valueOf(scores[1]); //this is line 388..
        ArrayList<String> players = null;


        if ((players = topTen.get(score)) == null) {
            players = new ArrayList<String>(1);
            players.add(scores[0]);
            topTen.put(Integer.valueOf(scores[1]), players);
        }
        else {
            players.add(scores[0]);
        }

    }

    for (Integer score : topTen.descendingKeySet()) {
        for (String player : topTen.get(score)) {
            try {
                bw.append(player + "Score: " + score + "n");
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
    br.close();
    bw.close();
    try {
        makeNewScoreTable();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

Can someone show me what I am doing wrong or missing? It worked well before, and suddenly it gives this.

Answer

In the following code, the line.split doesn’t seem to be returning you an array of size 2 (or more as per your requirement).

String[] scores = line.split("Points: ");
Integer score = Integer.valueOf(scores[1]);

You can get rid of the issue by checking the size of the array before trying to access an element from it e.g.

String[] scores = line.split("Points: ");
Integer score;
if(scores.length >= 2) {
    score = Integer.valueOf(scores[1]);
    //...
}

However, I would recommend you to find why line.split is not returning you an array of size 2 (or more as per your requirement). You need to check the content of the file.



Source: stackoverflow