8 Replies - 354 Views - Last Post: 01 March 2012 - 10:25 AM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Can someone check over this code for me?

Posted 01 March 2012 - 01:27 AM

I am trying to finish up a project for my programming class but i am having some difficulty, there are some runtime errors that occur when i try and run the program. The errors start with "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" but i dont see why that is coming up because what the error points to on the next line is something that shouldnt happen unless a button is pressed.

These are the classes i have currently:

/**
 * Project One
 * @author 
 */
public class ProjectOne {

    public static void main(String[] args) {
       TicketForm jframe;
       jframe = new TicketForm();
       jframe.setVisible(true);
    }
}



Basic main class that merely calls the JFrame and then is basically done.

/**
 * Ticket Form Class
 * @author 
 */

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

public class TicketForm extends JFrame implements ActionListener {
    
        
    // Private class constants
    private static final int Frame_Width = 400;
    private static final int Frame_Height = 500;
    private static final int Frame_X = 150;
    private static final int Frame_Y = 250;
    

    private JButton btnReport;
    private JButton btnReset;
    private JTextField txtCountA;
    private JTextField txtCountB;
    private JTextField txtCountC;
    private JTextField txtPriceA;
    private JTextField txtPriceB;
    private JTextField txtPriceC;
    private JLabel lblBlank;
    private JLabel lblCount;
    private JLabel lblPrice;
    private JLabel lblSeatA;
    private JLabel lblSeatB;
    private JLabel lblSeatC;
    private JTextArea txtReport;
    
    
    Seat countA;
    Seat countB;
    Seat countC;
    Seat priceA;
    Seat priceB;
    Seat priceC;
    Report report;
    

    public TicketForm() {
        
        // Initializing JFrame properties
        this.setTitle("Concert Ticket Calculator");
        this.setSize(Frame_Width, Frame_Height);
        this.setLocation(Frame_X, Frame_Y);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Gain reference to the contentPane object of this JFrame
        Container contentPane = this.getContentPane();
        
        // Set the layout manager of the contentPane
        contentPane.setLayout(new BorderLayout());
        
         // Start creating a GUI control object :)/>
        btnReport = new JButton("Create Report");
        btnReport.addActionListener(this);
        btnReset = new JButton("Reset");
        btnReset.addActionListener(this);
        lblCount = new JLabel("Count");
        lblPrice = new JLabel("Price ($)");
        lblSeatA = new JLabel("Enter for seat A:");
        lblSeatB = new JLabel("Enter for seat B:");
        lblSeatC = new JLabel("Enter for seat C:");
        lblBlank = new JLabel("");
        txtCountA = new JTextField();
        txtCountB = new JTextField();
        txtCountC = new JTextField();
        txtPriceA = new JTextField();
        txtPriceB = new JTextField();
        txtPriceC = new JTextField();
        txtReport = new JTextArea();
        report = new Report();
        
        // Construct a JPanel object to hold input controls
        JPanel dataPane = new JPanel();
        dataPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Enter Data"), BorderFactory.createEmptyBorder(10,10,10,10)));
        dataPane.setLayout(new GridLayout(4, 3, 5, 5));
        dataPane.add(lblBlank);
        dataPane.add(lblCount);
        dataPane.add(lblPrice);
        dataPane.add(lblSeatA);
        dataPane.add(txtCountA);
        dataPane.add(txtPriceA);
        dataPane.add(lblSeatB);
        dataPane.add(txtCountB);
        dataPane.add(txtPriceB);
        dataPane.add(lblSeatC);
        dataPane.add(txtCountC);
        dataPane.add(txtPriceC);
        contentPane.add(dataPane, BorderLayout.NORTH);
        
        
        JPanel reportPane = new JPanel();
        reportPane.setBorder(BorderFactory.createTitledBorder("Report"));
        reportPane.add(txtReport);
        contentPane.add(reportPane, BorderLayout.CENTER);
        
        JPanel buttonPane = new JPanel();
        buttonPane.add(btnReport);
        buttonPane.add(btnReset);
        contentPane.add(buttonPane, BorderLayout.SOUTH);  
    }
    
    public void actionPerformed(ActionEvent event) {
        
        
        if (event.getSource() == btnReport) {
            countA.setCountA(txtCountA.getText());
            countB.setCountB(txtCountB.getText());
            countC.setCountC(txtCountC.getText());
            priceA.setPriceA(txtPriceA.getText());
            priceB.setPriceB(txtPriceB.getText());
            priceC.setPriceC(txtPriceC.getText());
            txtReport.setText(report.getReport());
        } else {
            System.out.println("You clicked the reset");
              txtCountA.setText(""); 
              txtCountB.setText(""); 
              txtCountC.setText(""); 
              txtPriceA.setText("");
              txtPriceB.setText("");
              txtPriceC.setText("");
              txtReport.setText("");
        }

    }

}



Thats my JFrame class that sets everything up.

/**
 * Seat Class
 * @author 
 */
public class Seat {
    
    private int cA;
    private int cB;
    private int cC;
    private int pA;
    private int pB;
    private int pC;
    private int totalA;
    private int totalB;
    private int totalC;
    private int totalS;
    

    // Get and Set variables
    public void setCountA(String countA) {
        int cA = Integer.parseInt(countA);
        int totalA = (cA * pA);
    }
    
    public int getCountA() {
        return cA;
    }
    
    public int getTotalA() {
        return totalA;
    }
    
    public void setCountB(String countB) {
        int cB = Integer.parseInt(countB);
        int totalB = (cB * pB);
    }
    
    public int getCountB() {
        return cB;
    }
    
    public int getTotalB() {
        return totalB;
    }
    
    public void setCountC(String countC) {
        int cC = Integer.parseInt(countC);
        int totalC = (cC * pC);
    }
    
    public int getCountC() {
        return cC;
    }
    
    public int getTotalC() {
        return totalC;
    }
    
    
    public void setPriceA(String priceA) {
        int pA = Integer.parseInt(priceA);
    }
    
    public int getPriceA() {
        return pA;
    }
    
    public void setPriceB(String priceB) {
        int pB = Integer.parseInt(priceB);
    }
    
    public int getPriceB() {
        return pB;
    }
    
    public void setPriceC(String priceC) {
        int pC = Integer.parseInt(priceC);
    }
    
    public int getPriceC() {
        return pC;
    }
    public void setTotalS() {
        totalS = (totalA + totalB + totalC);
    }
    
    public int getTotalS() {
        return totalS;
    }
    
    
}



This is a seat class, it's meant to do the math for the whole thing.

/**
 * Report Class
 * @author 
 */
public class Report {


    Seat countA;
    Seat countB;
    Seat countC;
    Seat priceA;
    Seat priceB;
    Seat priceC;
    Seat totalA;
    Seat totalB;
    Seat totalC;
    Seat totalS;
    
     
    public String getReport() {
        
    int seatCountA = countA.getCountA();
    int seatCountB = countB.getCountB();
    int seatCountC = countC.getCountC();
    int seatPriceA = priceA.getPriceA();
    int seatPriceB = priceB.getPriceB();
    int seatPriceC = priceC.getPriceC();
    int seatTotalA = totalA.getTotalA();
    int seatTotalB = totalB.getTotalB();
    int seatTotalC = totalC.getTotalC();
    int seatTotalS = totalS.getTotalS();
    
    String report = ("Tickets Sold Price Total\n"
            + "------------ ----- -----\n"
            + "Seat A " + seatCountA + " $" + seatPriceA + " $" + seatTotalA + "\n"
            + "Seat B " + seatCountB + " $" + seatPriceB + " $" + seatTotalB + "\n"
            + "Seat C " + seatCountC + " $" + seatPriceC + " $" + seatTotalC + "\n"
            + "\n"
            + "Total Sales: $" + seatTotalS);
        

        return report;
    }
    

}



And finally the Report class which is just supposed to take the data and generate the output.

The whole project is a form that calculates the total sales based on the number of each type of ticket sold: A, B and C. And the value of each ticket, all of which are set by the user in 6 text fields. The seat class should take the data from the ticketform class, multiply the number of tickets by the cost for each seat type and put them into new variables. The report class is then supposed to take the variables from the seat class and generate a report based on it and return it so it can be outputted through the ticketform class.

I know it's probably a mess but i've only been learning Java for less than 2 months so far. So can anyone point out whats going on with this? Far as i can see it shouldnt give a null error for values in the Report class because they arent actually called until after the button is clicked. Or thats how it's supposed to be anyway.

Is This A Good Question/Topic? 0
  • +

Replies To: Can someone check over this code for me?

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Can someone check over this code for me?

Posted 01 March 2012 - 02:53 AM

It will take a while to read all that code, can you please tell us the class and line number where the exception is occurring?
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Can someone check over this code for me?

Posted 01 March 2012 - 04:05 AM

Yes, please post error messages exactly as they appear, copied and pasted.

Just a guess:

I don't see where the variables below are defined in TicketForm:

Seat countA;
Seat countB;
Seat countC;
Seat priceA;
Seat priceB;
Seat priceC;
Was This Post Helpful? 0
  • +
  • -

#4 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Can someone check over this code for me?

Posted 01 March 2012 - 09:35 AM

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TicketForm.actionPerformed(TicketForm.java:114)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.window.dispatchEventImpl(window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 18 seconds)

View PostGregBrannon, on 01 March 2012 - 04:05 AM, said:

Yes, please post error messages exactly as they appear, copied and pasted.

Just a guess:

I don't see where the variables below are defined in TicketForm:

Seat countA;
Seat countB;
Seat countC;
Seat priceA;
Seat priceB;
Seat priceC;

Those are so i can use get and set methods in the action performed event. I guess they are objects or instances of objects, cant remember exactly.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,362
  • Joined: 19-March 11

Re: Can someone check over this code for me?

Posted 01 March 2012 - 09:43 AM

Looking over it quickly, it looks like countA has not been initialized.

You can test this by putting in an apppropriate println at the head of that actionPerformed method (ie, if countA==null, print "Hey! countA is null! gonna crash now!")


Quote

Those are so i can use get and set methods in the action performed event. I guess they are objects or instances of objects, cant remember exactly.


Yes, they are instances of Seat. Or they would be if you initialized them. Sounds to me like you're having trouble with your design. If you declare objects, and you don't know why, it's time to step back and spend a little time at the whiteboard. Don't write more code at this point, you'll only get more tangled up than you are already.

This post has been edited by jon.kiparsky: 01 March 2012 - 09:45 AM

Was This Post Helpful? 1
  • +
  • -

#6 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Can someone check over this code for me?

Posted 01 March 2012 - 10:01 AM

View Postjon.kiparsky, on 01 March 2012 - 09:43 AM, said:

Looking over it quickly, it looks like countA has not been initialized.

You can test this by putting in an apppropriate println at the head of that actionPerformed method (ie, if countA==null, print "Hey! countA is null! gonna crash now!")


Quote

Those are so i can use get and set methods in the action performed event. I guess they are objects or instances of objects, cant remember exactly.


Yes, they are instances of Seat. Or they would be if you initialized them. Sounds to me like you're having trouble with your design. If you declare objects, and you don't know why, it's time to step back and spend a little time at the whiteboard. Don't write more code at this point, you'll only get more tangled up than you are already.

But i cant set those to anything because they are instances of Seat and it wont allow me to set them to a number or a string. And really when the button is clicked they should immeadiately be filled with whatever numbers have been put in the text boxes, so it shouldnt be null. CountA and the others like it should only be used as variables to send the values to the set method in the Seat class
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Can someone check over this code for me?

Posted 01 March 2012 - 10:05 AM

I don't quite follow your logic, but just as you declare and define other objects, so must you also with these before you can use them:

Seat countA;

THEN

countA = new Seat();

Or, all in one line:

Seat countA = new Seat();

Until you finish that process, they're all null, they don't exist in a form that your program can use. There's room set aside for them in memory, but there's nothing there.
Was This Post Helpful? 1
  • +
  • -

#8 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Can someone check over this code for me?

Posted 01 March 2012 - 10:24 AM

View PostGregBrannon, on 01 March 2012 - 10:05 AM, said:

I don't quite follow your logic, but just as you declare and define other objects, so must you also with these before you can use them:

Seat countA;

THEN

countA = new Seat();

Or, all in one line:

Seat countA = new Seat();

Until you finish that process, they're all null, they don't exist in a form that your program can use. There's room set aside for them in memory, but there's nothing there.

Ok i figured out that problem, now it doesnt crash but i just need to figure out why it isnt actually getting the numbers from the text boxes the way it should be.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,362
  • Joined: 19-March 11

Re: Can someone check over this code for me?

Posted 01 March 2012 - 10:25 AM

It'd probably be a good idea to post the revised code, then.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1