0

I'm trying to make a simple GUI program to calculate theater revenue (gross and net for adult/kids/total). I keep getting a very long list of runtime errors and I'm following my book's instructions as close as possible so I have no idea what is causing them.

Here's the code:

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

When I run it and hit the Calculate button I get the following errors:

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)
1
  • input1 = adultTicketPriceTextField.getText(); I'm trying to get the data from the text field Commented Oct 15, 2014 at 22:51

1 Answer 1

3

You have a variable shadowing issue, that is, you declare a bunch of instance fields....

public class TheaterRevenue extends JFrame {

    private JTextField adultTicketPriceTextField;
    private JTextField adultTicketSoldTextField;
    private JTextField childTicketPriceTextField;
    private JTextField childTicketSoldTextField;

But then redeclare them as local variables (within your constructor)

public TheaterRevenue() {
    //...
    //TextFields
    JTextField adultTicketPriceTextField = new JTextField(20);
    JTextField adultTicketSoldTextField = new JTextField(20);
    JTextField childTicketPriceTextField = new JTextField(20);
    JTextField childTicketSoldTextField = new JTextField(20);

This means that when the actionPerformed method tries to access the values from these instance fields

public void actionPerformed(ActionEvent e) {
    //..
    input1 = adultTicketPriceTextField.getText();

The field is still null.

To fix it, remove the declarations within you constructor and make sure you are initialising the instance fields correctly...

public TheaterRevenue() {
    //...
    //TextFields
    adultTicketPriceTextField = new JTextField(20);
    adultTicketSoldTextField = new JTextField(20);
    childTicketPriceTextField = new JTextField(20);
    childTicketSoldTextField = new JTextField(20);
Sign up to request clarification or add additional context in comments.

5 Comments

Now it says those fields cannot be resolved to a variable. Do I have some visibility issues?
Works fine for me...you did remove the JTextField .... from within the constructor and the instance fields?
I removed the declarations from the constructor entirely and then removed JTextField from the class body.
You will need to the instance fields. You need to initialize the fields within the constructor
"you it did fix it" Oh whoops, wasn't suppose to do that <nudge><nudge> ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.