NullPointerExceptionException; "AWT-EventQueue-0"



当我运行程序时,我总是在控制台中收到这个错误:

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());  

最后一次通过循环时,i的值是多少?当你在其中加1时会发生什么?

我不清楚为什么你会得到一个包裹IndexOutOfBoundsExceptionNullPointerException,但问题是:

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());  

注意这里的呼叫:

messwerte.get(i+1)

i等于messwerte.size() - 1时,则i + 1将等于messwerte.size(),因此它超出了列表的范围。

我怀疑你想使用:

for (int i = 0; i < messwerte.size() - 1; i++) {

或者使用原始边界,但是使用i - 1i的索引而不是ii + 1

相关内容

  • 没有找到相关文章

最新更新