5 Replies - 260 Views - Last Post: 03 December 2017 - 04:33 AM Rate Topic: -----

#1 trueblue1fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 17

Theater GUI

Posted 02 December 2017 - 11:38 PM

Hi first time user here I have an issue with my GUI I'm and I know over looking how to fix it but.

package Chapter12;

import javax.swing.*;
import java.awt.event.*;

public class TheaterGUI extends JFrame {
   
    JTextField adultTicketPriceTextField;
    JTextField adultTicketSoldTextField; 
    JTextField childTicketPriceTextField;
    JTextField childTicketSoldTextField;
    

        public TheaterGUI()
    {
        
         
    final int windowWidth = 350;
    final int windowHeight = 275;
     
    setTitle("Theater Revenue ");  
    setSize(windowWidth, windowHeight);
    JButton button = new JButton("Calculate ");
    button.addActionListener(new buttonListener());
    JTextField adultTicketPriceText;  
    adultTicketPriceText = new JTextField(20);
    JTextField adultTicketSoldText;   
    adultTicketSoldText = new JTextField(20);
    JTextField childTicketPriceText;
    childTicketPriceText = new JTextField(20);
    JTextField childTicketSoldText;
    childTicketSoldText = new JTextField(20);
    JLabel adultPrice;

    adultPrice = new JLabel("Adult Ticket Price:  ");
    JLabel adultSales;
    adultSales = new JLabel ("Adult Tickets Sold:  ");
    JLabel childPrice;
    childPrice = new JLabel ("Child Ticket Price:  ");
    JLabel childSales;
    childSales = new JLabel ("Child Tickets Sold:  ");
    JLabel spacer;
    spacer = new JLabel ("  ");
    JPanel panel1 = new JPanel();
    panel1.add(adultPrice);
    panel1.add(adultTicketPriceText);
    panel1.add(adultSales);
    panel1.add(adultTicketSoldText);
    panel1.add(childPrice);
    panel1.add(childTicketPriceText);
    panel1.add(childSales);
    panel1.add(childTicketSoldText);
    panel1.add(spacer);
    panel1.add(button);
    add(panel1);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);

    }

    private class buttonListener implements ActionListener

    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
        String adultTickets;
        double adultTicketsNum;
        String adultSales;
        double adultSalesNum;
        String childTickets;
        double childTicketsNum;
        String childSales;
        double childSalesNum;
        double adultGross;
        double adultNet;
        double childGross;
        double childNet;
        double totalGross;
        double totalNet;
        double constant = 0.20;
         
        adultTickets = adultTicketPriceTextField.getText();
        adultSales = adultTicketSoldTextField.getText();
        childTickets = childTicketPriceTextField.getText();
        childSales = childTicketSoldTextField.getText();

        adultTicketsNum = Double.parseDouble(adultTickets);
        adultSalesNum = Double.parseDouble(adultTickets);
        childTicketsNum = Double.parseDouble(childTickets);
        childSalesNum = Double.parseDouble(childTickets);

         
        adultGross = adultTicketsNum * adultSalesNum;
        adultNet = adultGross * constant;
        childGross = childTicketsNum * childSalesNum;
        childNet = childGross * constant;
        totalGross = adultGross + childGross;
        totalNet = adultNet + childNet;

            JOptionPane.showMessageDialog(null, 
                    "The Gross for Adult tickets was " + adultGross + "\n" +
                    " The Net for Adult Tickets was " + adultNet + "\n" +
                    " The Gross for Children Tickets was " +childGross + "\n" +
                    " The Net for Child Tickets was " + childNet + "\n" +
                    " The Total Gross was " + totalGross + "\n" +
                    " The Total Net was " + totalNet);
        }
        
        }
    public static void main(String[] args){
        new TheaterGUI();
    }
}




It's an ititalizing error but I can't figure out where or how to fix it any thoughts??

Is This A Good Question/Topic? 0
  • +

Replies To: Theater GUI

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: Theater GUI

Posted 02 December 2017 - 11:39 PM

Can you be more specific as to your error? Please post your errors exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#3 trueblue1fan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 17

Re: Theater GUI

Posted 02 December 2017 - 11:43 PM

Yes sorry.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at Chapter12.TheaterGUI$buttonListener.actionPerformed(TheaterGUI.java:84)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2236)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2294)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
	at java.awt.Container.dispatchEventImpl(Container.java:2280)
	at java.awt.window.dispatchEventImpl(window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: Theater GUI

Posted 02 December 2017 - 11:46 PM

In your actionPerformed() method, you attempt to use the variable adultTicketPriceTextField, which you never initialize.

Instead, your constructor, you declare a local variable:
JTextField adultTicketPriceText; 
adultTicketPriceText = new JTextField(20);



The same applies for the other JTextField instance variables.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7009
  • View blog
  • Posts: 14,661
  • Joined: 16-October 07

Re: Theater GUI

Posted 03 December 2017 - 04:13 AM

You have this really nasty habit of declaring variables without initializing them. And, apparently, your IDE is letting you down by not highligthing unused variables.

Here's a quick refactor:
public class TheaterGUI extends JFrame {
    // are these part of your instnace, then?
    // set the access
    private JTextField adultTicketPriceTextField;
    // never used
    // private JTextField adultTicketSoldTextField;
    private JTextField childTicketPriceTextField;
    // never used
    // private JTextField childTicketSoldTextField;

    public TheaterGUI() {
        final int windowWidth = 350;
        final int windowHeight = 275;

        setTitle("Theater Revenue ");
        setSize(windowWidth, windowHeight);
        JButton button = new JButton("Calculate ");
        button.addActionListener(new ButtonListener());
        // JTextField adultTicketPriceText;
        JTextField adultTicketPriceText = new JTextField(20);
        // JTextField adultTicketSoldText;
        JTextField adultTicketSoldText = new JTextField(20);
        // JTextField childTicketPriceText;
        JTextField childTicketPriceText = new JTextField(20);
        // JTextField childTicketSoldText;
        JTextField childTicketSoldText = new JTextField(20);
        //JLabel adultPrice;
        JLabel adultPrice = new JLabel("Adult Ticket Price:  ");
        // JLabel adultSales;
        JLabel adultSales = new JLabel("Adult Tickets Sold:  ");
        // JLabel childPrice;
        JLabel childPrice = new JLabel("Child Ticket Price:  ");
        // JLabel childSales;
        JLabel childSales = new JLabel("Child Tickets Sold:  ");
        // JLabel spacer;
        JLabel spacer = new JLabel("  ");
        // wtf init only one panel?
        // JPanel panel1 = new JPanel();
        // perhaps you wanted flow layout
        java.awt.Container panel1 = this.getContentPane();
        panel1.setLayout(new java.awt.FlowLayout());
        panel1.add(adultPrice);
        panel1.add(adultTicketPriceText);
        panel1.add(adultSales);
        panel1.add(adultTicketSoldText);
        panel1.add(childPrice);
        panel1.add(childTicketPriceText);
        panel1.add(childSales);
        panel1.add(childTicketSoldText);
        panel1.add(spacer);
        panel1.add(button);
        // add(panel1);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);

    }
    
    
    // bad name
    // private class buttonListener implements ActionListener {

    class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            /*
        declare and asign, not this
            String adultTickets;
            double adultTicketsNum;
            String adultSales;
            double adultSalesNum;
            String childTickets;
            double childTicketsNum;
            String childSales;
            double childSalesNum;
            double adultGross;
            double adultNet;
            double childGross;
            double childNet;
            double totalGross;
            double totalNet;
             */
            // thne make is a constant and what the hell is it
            // double constant = 0.20;
            final double MARKUP = 0.20;

            // to not fake yourself out, comment these out fro the moement
            // final String adultTickets = adultTicketPriceTextField.getText();
            // never used
            // final String adultSales = adultTicketSoldTextField.getText();
            // final String childTickets = childTicketPriceTextField.getText();
            // never used 
            // final String childSales = childTicketSoldTextField.getText();

            final double adultTicketsNum = Double.parseDouble(adultTickets);
            // oops?
            final double adultSalesNum = Double.parseDouble(adultTickets);
            final double childTicketsNum = Double.parseDouble(childTickets);
            final double childSalesNum = Double.parseDouble(childTickets);

            double adultGross = adultTicketsNum * adultSalesNum;
            double adultNet = adultGross * MARKUP;
            double childGross = childTicketsNum * childSalesNum;
            double childNet = childGross * MARKUP;
            double totalGross = adultGross + childGross;
            double totalNet = adultNet + childNet;

            JOptionPane.showMessageDialog(null,
                    "The Gross for Adult tickets was " + adultGross + "\n"
                    + " The Net for Adult Tickets was " + adultNet + "\n"
                    + " The Gross for Children Tickets was " + childGross + "\n"
                    + " The Net for Child Tickets was " + childNet + "\n"
                    + " The Total Gross was " + totalGross + "\n"
                    + " The Total Net was " + totalNet);
        }

    }

    public static void main(String[] args) {
        new TheaterGUI();
    }
}




In your ButtonListener, I've commented out the references to those form level vars. Why? Again, if you IDE doesn't suck, it should now flag those vars as never used, which should show clearly where you have a problem.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7009
  • View blog
  • Posts: 14,661
  • Joined: 16-October 07

Re: Theater GUI

Posted 03 December 2017 - 04:33 AM

Also, I'd recommend your class mark those instance fields as final, e.g.
private final JTextField adultTicketPriceTextField;



This flag not only prevents further change, but also requires the value be initialized in the constructor, immediately revealing your issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1