由于文本字段,ActionListener中的空指针?

时间:2013-03-02 02:24:29

标签: java swing nullpointerexception jtextfield

作为java swing GUI界面和我在学校即将推出的Geometry课程的介绍,我决定为该课程制作一个“特殊”计算器。 现在我有一个空指针,我不太明白是什么导致它。我编程的方式适用于我的其他课程,为什么它不能在这里工作?

无论如何,这里是错误所在的两个类,另外两个有效,我不明白我做了什么不同。 这是错误:

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

这是形状的类(矩形棱镜)

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

这是它的听众的课程

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) {

    }

    }

然后我们有工作广场和它的倾听者

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

}

听众

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 + ".");
    }

}

所有我想知道的是为什么它会产生以及如何解决它,谢谢。

1 个答案:

答案 0 :(得分:5)

这一切都在堆栈跟踪中:

Caused by: java.lang.NullPointerException
at geohelper.RPrismListener.<init>(RPrismListener.java:21)

查看RPrismListener构造函数:

public RPrismListener(JButton jb1, JTextField Jtxt1, JTextField Jtxt2, JTextField Jtxt3) {
    listenbutton = jb1;
    listenfield[0] = Jtxt1;
    listenfield[1] = Jtxt2;
    listenfield[2] = Jtxt3;
}

listenField永远不会被初始化,所以它是空的。

在构造函数的开头添加:

listenfield = new JTextField[3];