繁体   English   中英

NullPointerExceptionException; “ AWT-EventQueue-0”

[英]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());  

我最后一次经过循环的值是多少? 将1加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() - 1i + 1将等于messwerte.size() ,因此它不在列表的范围之内。

怀疑您想使用:

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

...或使用原始范围,但使用i - 1i索引而不是ii + 1索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM