NullPointerExceptionException; " AWT-EventQueue的-0"

时间:2015-02-19 19:04:19

标签: java eclipse graphics nullpointerexception

我在运行程序时总是在控制台中出现此错误:

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

  }     
}

2 个答案:

答案 0 :(得分:4)

看看这一行:

 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 :(得分:3)

我不清楚为什么你会NullPointerException包裹IndexOutOfBoundsException,但这就是问题所在:

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的索引。