I always get this error in the console when I'm running my program:
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)
The program works like it should do, showing some graphs in a coordinate system but I want to know why this messages shows up and fix it.
Here's my code:
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());
}
}
}
It's not clear to me why you'd get a NullPointerException
wrapping the IndexOutOfBoundsException
, but this is the problem:
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());
Note the call here:
messwerte.get(i+1)
When i
is equal to messwerte.size() - 1
, then i + 1
will be equal to messwerte.size()
, so it's out of the bounds of the list.
I suspect you want to either use:
for (int i = 0; i < messwerte.size() - 1; i++) {
... or use the original bounds, but use indexes of i - 1
and i
instead of i
and i + 1
.
See more on this question at Stackoverflow