import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class TheatreRevenue  extends JFrame {

    /**
     * 
     */

    final double percent = .20;
    double adultCost;
    private JTextField label2;
    private JTextField label4;
    private JTextField label6;
    private JTextField label8;
    private JButton button1;
    private final int WINDOW_WIDTH = 400;
    private final int WINDOW_HEIGHT = 200;


    /**
     * @param args
     */

    public TheatreRevenue(){
        setTitle("Theatre Revenue");

        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new GridLayout(5,2));

        button1 = new JButton("Calculate Revenue");
        button1.addActionListener(new CalcButtonListener());

        JLabel label1 = new JLabel("Enter the price of one adult ticket : ");
        JTextField label2 = new JTextField(10);
        JLabel label3 = new JLabel("Enter the number of adult tickets sold : ");
        JTextField label4 = new JTextField(10);
        JLabel label5 = new JLabel("Enter the price of one child ticket : ");
        JTextField label6 = new JTextField(10);
        JLabel label7 = new JLabel("Enter the number of child tickets sold : ");
        JTextField label8 = new JTextField(10);



        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
        JPanel panel6 = new JPanel();
        JPanel panel7 = new JPanel();
        JPanel panel8 = new JPanel();
        JPanel panel9 = new JPanel();

        panel1.add(label1);
        panel2.add(label2);
        panel3.add(label3);
        panel4.add(label4);
        panel5.add(label5);
        panel6.add(label6);
        panel7.add(label7);
        panel8.add(label8);

        panel9.add(button1);

        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);
        add(panel5);
        add(panel6);
        add(panel7);
        add(panel8);
        add(panel9);

        setVisible(true);


    }

    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {


            String adultPrice;
            String adultAmount;
            String childPrice;
            String childAmount;

            //System.out.println("hello");

            adultPrice = label2.getText();
            adultCost=Double.parseDouble(adultPrice);








            System.out.print("got here");

            adultAmount = label4.getText();

            double grossAdultRevenue =  (adultCost)* Double.parseDouble(adultAmount);
            double netAdultRevenue = (grossAdultRevenue) * percent;

            childPrice = label6.getText();

            childAmount = label8.getText();

            double grossChildRevenue = Double.parseDouble (childPrice)* Double.parseDouble(childAmount);

            double netChildRevenue = grossChildRevenue * percent;

            double grossRevenue = grossChildRevenue+grossAdultRevenue;

            double netRevenue = netAdultRevenue+netChildRevenue;

            JOptionPane.showMessageDialog(null, "Total gross adult revenue : " + grossAdultRevenue + "\n" + 
                                                "Total net adult revenue : " + netAdultRevenue + "\n" + 
                                                "Total gross child revenue : " + grossChildRevenue + "\n" +
                                                "Total net child revenue : " + netChildRevenue + "\n" +
                                                "Total gross revenue of sales : " + grossRevenue + "\n" +
                                                "Total net revenue of sales : " + netRevenue);

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

        new TheatreRevenue();

        // TODO Auto-generated method stub

    }

}

Every time I try and run my program I get the following error message. I don't know if its that i have a null pointer messing with my button or the information inside of my button. I also have no idea how i would fix it in either case. Please Help!!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TheatreRevenue$CalcButtonListener.actionPerformed(TheatreRevenue.java:99)
    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.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)

Recommended Answers

All 8 Replies

can u properly indent your code.Its quite confusing.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TheatreRevenue$CalcButtonListener.actionPerformed(TheatreRevenue.java:99)

At that line and file: TheatreRevenue.java:99 you are using something which is null. Go to tha line and see what is null and make sure it is initialized.

Also the code tags you are using should end with

In constructor you redefine the label2,label4,label6, and label8 as local variable of the type JTextField, which is wrong. You have already declared them as attributes.

You should thus replace the 8 lines of code in the constructor by the following code:

JLabel label1 = new JLabel("Enter the price of one adult ticket : ");
label2 = new JTextField(10);
JLabel  label3 = new JLabel("Enter the number of adult tickets sold : ");
label4 = new JTextField(10);
JLabel label5 = new JLabel("Enter the price of one child ticket : ");
label6 = new JTextField(10);
JLabel label7 = new JLabel("Enter the number of child tickets sold : ");
label8 = new JTextField(10);

toferdagofer, As I said before, within constructor you re-define/re-declare the label2,label4,label6, and label8 as local variable of the type JTextField, which is wrong. Why? While you re-declare and initiate them as local variables, the attributes of the same names, as you declared for the class, are in fact not initiated. Therefore, the run time error message : “java.lang.NullPointerException” is thrown when executing the line of code:

adultPrice = label2.getText() ; //(line 99)

because the attribute label2 is null.
I also have a comment on the naming of variables: label2,…label8, which are in fact TextField’s instances. I would name them as textfield1,... textfield4 so that their names are meaningful.

thank you this was all incredibly helpful and i have definatly found a place that i want to go if i have questions. Being that this is my first java class i have taken this will be very helpful in the future

Not at all. Please make sure you mark the thread as solved if your queries have been answered.

Not at all. Please make sure you mark the thread as solved if your queries have been answered.

Hi,

I am currently about to start a project in designing a JAVA GUI; Cinema booking system.

Please does anyone have an example code I can have a look at in order to see what structures to cover and how to build my own code?

Hi,

I am currently about to start a project in designing a JAVA GUI; Cinema booking system.

Please does anyone have an example code I can have a look at in order to see what structures to cover and how to build my own code?

Please do not revive old/solved threads. rather go and start a new thread for your question, you will also get a lot more help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.