运行程序时,总是在控制台中收到此错误:
java.lang.NullPointerExceptionException in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 74, Size: 74
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Graph.paint(Graph.java:47)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
该程序按应有的方式工作,在坐标系中显示一些图形,但我想知道为什么显示此消息并进行修复。
这是我的代码:
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class Graph extends Panel {
public static void main (String args[]) {
Graph Heretsu = new Graph();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize (820, 820);
f.add (Heretsu);
f.setVisible(true);
}
public void paint (Graphics g) {
g.setColor (Color.BLUE);
g.drawLine (5, 5, 5, 600);
g.drawLine (5, 600, 600, 600);
for (int i = 20; i < 600; i += 20) {
g.drawLine (i, 598, i, 602);
g.drawLine (3, i, 7, i);
}
g.drawLine (605, 600, 595, 595);
g.drawLine (605, 600, 595, 605);
g.drawLine (5, 0,0, 5);
g.drawLine (5, 0, 10, 5);
g.setFont(new Font("Arial", Font.HANGING_BASELINE, 15));
g.setColor (Color.BLACK);;
g.drawString ("Heretsu Enterprises",650, 50);
Untersuchung untersuchung = new Untersuchung();
Auslesen.lesen();
ArrayList messwerte = new ArrayList();
messwerte = untersuchung.getMesswerte();
System.out.println(messwerte.size());
for (int i = 1; i < messwerte.size(); i ++ ) {
g.setColor(Color.MAGENTA);
g.drawLine((i*7),((Messwert)messwerte.get(i)).getPuls(),(i+1)*7, ((Messwert)messwerte.get(i+1)).getPuls());
g.drawLine((i*7),((Messwert)messwerte.get(i)).getDiastole(),(i+1)*7, ((Messwert)messwerte.get(i+1)).getDiastole());
g.drawLine((i*7),((Messwert)messwerte.get(i)).getSystole(),(i+1)*7, ((Messwert)messwerte.get(i+1)).getSystole());
}
}
}
最佳答案
看这行:
for (int i = 1; i < messwerte.size(); i ++ ) {
g.setColor(Color.MAGENTA);
g.drawLine((i*7),((Messwert)messwerte.get(i)).getPuls(),(i+1)*7, ((Messwert)messwerte.get(i+1)).getPuls());
我最后一次经过循环的值是多少?将1加1会发生什么?
关于java - NullPointerExceptionException; “AWT-EventQueue-0”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28614778/