java - Program won't run after a while, and I get this
问题描述
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.
解决方案
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.
推荐阅读
- amazon-web-services - 从正在运行的 EC2 实例生成 EC2 启动模板
- laravel-5 - 更改密码重置链接
- python-2.7 - Python中的嵌套列表比较
- json - 模型类中 JsonNode 属性的 springfox 招摇配置
- ag-grid - React ag grid - 事件以查找用户在滚动时是否到达网格末尾
- ios - 如何在 UINavigation 控制器中交换 2 个视图控制器
- html - 如何修复灯塔“链接没有可识别的名称”
- go - 接口如何表示 Golang 中的任何类型
- meteor - 当应用程序重定向到另一个应用程序 Meteor JS 时注销用户
- c# - Angular Azure B2C SSO 问题