As an introduction into java swing GUI interface and my upcoming Geometry class at school, I decided to make a "Special" Calculator for that class. Now I'm having a Null Pointer in it and I don't quite understand what's causing it. the way I programmed it worked on my other classes so why doesn't it work here?
Anyway here's the two classes the error is in and the other two that worked, I don't understand what I did differently. Here's the error:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at geohelper.GeoFrame.addToGUI(GeoFrame.java:90)
at geohelper.GeoFrame.showGUI(GeoFrame.java:42)
at geohelper.GeoHelper$1.run(GeoHelper.java:13)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.NullPointerException
at geohelper.RPrismListener.<init>(RPrismListener.java:21)
at geohelper.RecPrism.<clinit>(RecPrism.java:14)
... 17 more
Here's the class for the shape(Rectangular Prism)
package geohelper;
import java.awt.Container;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.DocumentListener;
public class RecPrism {
static final JButton rprismbutton = new JButton("Calculate");
static final JTextField rplength = new JTextField(10);
static final JTextField rpwidth = new JTextField(10);
static final JTextField rpheight = new JTextField(10);
private static ActionListener al = new RPrismListener(rprismbutton, rplength, rpwidth, rpheight);
private static DocumentListener dl = new RPrismListener(rprismbutton, rplength, rpwidth, rpheight);
public double volume(double length, double width, double height) {
return length*width*height;
}
public double surfacearea(double length, double width, double height) {
return (2*length*width)*(2*length*height)*(2*width*height);
}
public static void addGUI(Container pane) {
rprismbutton.addActionListener(al);
rplength.setText("Length");
rplength.setActionCommand("Calculate");
rplength.getDocument().addDocumentListener(dl);
rplength.getDocument().putProperty("Length", "Text Field");
rplength.addActionListener(al);
rpwidth.setText("Width");
rpwidth.setActionCommand("Calculate");
rpwidth.getDocument().addDocumentListener(dl);
rpwidth.getDocument().putProperty("Width", "Text Field");
rpwidth.addActionListener(al);
rpheight.setText("Height");
rpheight.setActionCommand("Calculate");
rpheight.getDocument().addDocumentListener(dl);
rpheight.getDocument().putProperty("Height", "Text Field");
rpheight.addActionListener(al);
pane.add(rprismbutton);
pane.add(rplength);
pane.add(rpwidth);
pane.add(rpheight);
}
}
and Here's the class for it's Listener
package geohelper;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class RPrismListener implements ActionListener, DocumentListener {
private JButton listenbutton;
private JTextField listenfield[];
private String s[];
private boolean failinput = false;
private boolean hasinput[] = {false, false, false};
private double y, z;
private double x[];
private RecPrism rp = new RecPrism();
public RPrismListener(JButton jb1, JTextField Jtxt1, JTextField Jtxt2, JTextField Jtxt3) {
listenbutton = jb1;
listenfield[0] = Jtxt1;
listenfield[1] = Jtxt2;
listenfield[2] = Jtxt3;
}
@Override
public void actionPerformed(ActionEvent ae) {
for(int d = 0; d < 2; d++) {
if(hasinput[d]) {
s[d] = listenfield[d].getText();
x[d] = Double.parseDouble(s[d]);
} else {
GeoFrame.GLabel.setText("one or more of the text fields haven't been changed.");
return;
}
}
y = rp.volume(x[0], x[1], x[2]);
z = rp.surfacearea(x[0], x[1], x[2]);
GeoFrame.GLabel.setText("The Volume is: " + y + ". The Surface area is: " + z +".");
}
@Override
public void insertUpdate(DocumentEvent de) {
if(de.getDocument().equals("Length")) {
hasinput[0] = true;
} if (de.getDocument().equals("Width")) {
hasinput[1] = true;
} if (de.getDocument().equals("Height")) {
hasinput[2] = true;
}
}
@Override
public void removeUpdate(DocumentEvent de) {
}
@Override
public void changedUpdate(DocumentEvent de) {
}
}
Then we've got the Working Square and it's listener
package geohelper;
import java.awt.Container;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Square {
double result;
static JButton squarebutton;
static JTextField squarefield;
private static ActionListener squarelistener = new SquareListener(squarebutton, squarefield);
public double area(double side) {
return side*side;
}
public double perimeter(double side) {
result = side*4;
return result;
}
public static void addGUI(Container pane) {
squarefield = new JTextField(10);
squarefield.setActionCommand("Calculate");
squarefield.addActionListener(squarelistener);
squarebutton = new JButton("Calculate");
squarebutton.addActionListener(squarelistener);
pane.add(squarebutton);
pane.add(squarefield);
}
}
The Listener
package geohelper;
import javax.swing.*;
import java.awt.event.*;
public class SquareListener implements ActionListener {
private JButton ListenButton;
private JTextField ListenField;
private double x;
private double y;
private double z;
private String s = "1";
private Square square = new Square();
public SquareListener(JButton par1, JTextField par2) {
this.ListenButton = par1;
this.ListenField = par2;
}
@Override
public void actionPerformed(ActionEvent ae) {
try {
s = Square.squarefield.getText();
} catch(NullPointerException npe) {
npe.printStackTrace();
}
x = Double.parseDouble(s);
y = square.area(x);
z = square.perimeter(x);
GeoFrame.GLabel.setText("The area is: " + y + ". The perimeter is: " + z + ".");
}
}
all I'm wondering is Why it's generated and how to fix it, Thank you.
It's all in the stack trace:
Caused by: java.lang.NullPointerException
at geohelper.RPrismListener.<init>(RPrismListener.java:21)
Look at RPrismListener
constructor:
public RPrismListener(JButton jb1, JTextField Jtxt1, JTextField Jtxt2, JTextField Jtxt3) {
listenbutton = jb1;
listenfield[0] = Jtxt1;
listenfield[1] = Jtxt2;
listenfield[2] = Jtxt3;
}
listenField
is never initialized, so it's null.
Add this at the start of your constructor:
listenfield = new JTextField[3];