我正在尝试制作一个简单的GUI程序来计算剧院收入(成人/儿童的总数和净额/总数)。我不断得到一个很长的运行时错误列表,我尽可能地遵循我的书的说明,所以我不知道是什么导致它们。
以下是代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TheaterRevenue extends JFrame
{
private JTextField adultTicketPriceTextField;
private JTextField adultTicketSoldTextField;
private JTextField childTicketPriceTextField;
private JTextField childTicketSoldTextField;
public TheaterRevenue()
{
setTitle("Theater Revenue");
setSize(400, 175);
JButton calcButton = new JButton("Calculate");
//Need JButton's Listener
calcButton.addActionListener(new calcButtonListener());
//TextFields
JTextField adultTicketPriceTextField = new JTextField(20);
JTextField adultTicketSoldTextField = new JTextField(20);
JTextField childTicketPriceTextField = new JTextField(20);
JTextField childTicketSoldTextField = new JTextField(20);
//TextField Labels (20 is field length, adjust as needed)
JLabel adultTickPrice = new JLabel("Adult Ticket Price");
JLabel adultTickSold = new JLabel ("Adult Tickets Sold");
JLabel childTickPrice = new JLabel ("Child Ticket Price");
JLabel childTickSold = new JLabel ("Child Tickets Sold");
//Panel Setup
JPanel mainPanel = new JPanel();
add(mainPanel);
mainPanel.add(adultTickPrice);
mainPanel.add(adultTicketPriceTextField);
mainPanel.add(adultTickSold);
mainPanel.add(adultTicketSoldTextField);
mainPanel.add(childTickPrice);
mainPanel.add(childTicketPriceTextField);
mainPanel.add(childTickSold);
mainPanel.add(childTicketSoldTextField);
mainPanel.add(calcButton);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class calcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input1, input2, input3, input4;
double adultTickPrice, adultTickSold, childTickPrice, childTickSold;
input1 = adultTicketPriceTextField.getText();
adultTickPrice = Double.parseDouble(input1);
input2 = adultTicketSoldTextField.getText();
adultTickSold = Double.parseDouble(input2);
input3 = childTicketPriceTextField.getText();
childTickPrice = Double.parseDouble(input3);
input4 = childTicketSoldTextField.getText();
childTickSold = Double.parseDouble(input4);
//Calculations
double adultGross, adultNet, childGross, childNet, totalGross, totalNet;
final double keep = 0.20;
adultGross = (adultTickPrice*adultTickSold);
adultNet = ((adultTickPrice*adultTickSold)*keep);
childGross = (childTickPrice*childTickSold);
childNet = ((childTickPrice*adultTickSold)*keep);
totalGross = (adultGross+childGross);
totalNet = (adultNet+adultGross);
JOptionPane.showMessageDialog(null, adultGross);
JOptionPane.showMessageDialog(null, adultNet);
JOptionPane.showMessageDialog(null, childGross);
JOptionPane.showMessageDialog(null, childNet);
JOptionPane.showMessageDialog(null, totalGross);
JOptionPane.showMessageDialog(null, totalNet);
}
}
public static void main(String[] args)
{
new TheaterRevenue();
}
}
当我运行它并按下计算按钮时,我收到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TheaterRevenue$calcButtonListener.actionPerformed(TheaterRevenue.java:53)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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)
答案 0 :(得分:3)
你有一个变量阴影问题,就是你声明了一堆实例字段......
public class TheaterRevenue extends JFrame {
private JTextField adultTicketPriceTextField;
private JTextField adultTicketSoldTextField;
private JTextField childTicketPriceTextField;
private JTextField childTicketSoldTextField;
但是然后将它们重新声明为局部变量(在构造函数中)
public TheaterRevenue() {
//...
//TextFields
JTextField adultTicketPriceTextField = new JTextField(20);
JTextField adultTicketSoldTextField = new JTextField(20);
JTextField childTicketPriceTextField = new JTextField(20);
JTextField childTicketSoldTextField = new JTextField(20);
这意味着当actionPerformed
方法尝试访问这些实例字段中的值时
public void actionPerformed(ActionEvent e) {
//..
input1 = adultTicketPriceTextField.getText();
该字段仍为null
。
要修复它,请删除构造函数中的声明,并确保正确初始化实例字段...
public TheaterRevenue() {
//...
//TextFields
adultTicketPriceTextField = new JTextField(20);
adultTicketSoldTextField = new JTextField(20);
childTicketPriceTextField = new JTextField(20);
childTicketSoldTextField = new JTextField(20);