public class BusPayment extends JFrame{
private JButton paynow;
private JButton cancel;
private JComboBox<String> jComboBox1;
private JFrame frame;
private JLabel label1, label2, label3, label4;
private JPanel panel1, panel2,panel3;
private JRadioButton destination1;
private JRadioButton destination2;
private JRadioButton destination3;
private JRadioButton destination4;
private JRadioButton destination5;
private JRadioButton destination6;
private JComboBox company;
private JTextField passengerstextfield;
public BusPayment (){
frame.setTitle("Express Bus Ticketing Management System");
frame.setSize(1400,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
buildPanel();
panel1.add(label1);
panel2.add(label2);
panel2.add(passengerstextfield);
panel2.add(label3);
panel2.add(company);
panel3.add(destination1);
panel3.add(destination2);
panel3.add(destination3);
panel3.add(destination4);
panel3.add(destination5);
panel3.add(destination6);
panel3.add(paynow);
panel3.add(cancel);
}
private void buildPanel(){
panel1 =new JPanel();
label1=new JLabel("Welcome to the Express Bus Ticketing System! ");
panel2=new JPanel();
label2=new JLabel("Number of Passenger(s): ");
passengerstextfield=new JTextField(8);
label3=new JLabel("Selected Bus Company: ");
final JComboBox<String> comp = new JComboBox<>(new String[] {"Company1", "Company2", "Company3", "Company4"});
panel3=new JPanel();
label4=new JLabel("Select a ticket: ");
destination1=new JRadioButton ("Destination 1 RM15");
JRadioButton des1 = new JRadioButton(Double.toString(15));
des1.setSelected(true);
des1.add(des1);
destination2=new JRadioButton ("Destination 2 RM18");
JRadioButton des2 = new JRadioButton(Double.toString(18));
des2.setSelected(true);
des2.add(des2);
destination3=new JRadioButton ("Destination 3 RM15");
JRadioButton des3 = new JRadioButton(Double.toString(15));
des3.setSelected(true);
des3.add(des3);
destination4=new JRadioButton ("Destination 4 RM12");
JRadioButton des4 = new JRadioButton(Double.toString(12));
des4.setSelected(true);
des4.add(des4);
destination5=new JRadioButton ("Destination 5 RM16");
JRadioButton des5 = new JRadioButton(Double.toString(16));
des5.setSelected(true);
des5.add(des5);
destination6=new JRadioButton ("Destination 6 RM13");
JRadioButton des6 = new JRadioButton(Double.toString(13));
des6.setSelected(true);
des6.add(des6);
paynow=new JButton ("Proceed");
paynow.addActionListener(new PayNowListener());
cancel=new JButton ("Cancel");
cancel.addActionListener(new CancelListener());
panel1.add(label1);
panel2.add(label2);
panel2.add(passengerstextfield);
panel2.add(label3);
panel2.add(comp);
panel3.add(des1);
panel3.add(des2);
panel3.add(des3);
panel3.add(des4);
panel3.add(des5);
panel3.add(des6);
panel3.add(paynow);
panel3.add(cancel);
}
private void destination1ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination2ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price); }
private void destination3ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination4ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination5ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination6ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private class PayNowListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae){
panel3.setBackground(Color.CYAN);
double passengers;
double price;
double tax;
double payment;
DecimalFormat nf = new DecimalFormat("0.00");
try{
passengers=Double.parseDouble(passengerstextfield.getText());
price=
tax=0.06;//6%
payment=passengers*price*tax+(passengers*price);
JOptionPane.showMessageDialog(null, "Sub-Total: " + nf.format(passengers*price));
JOptionPane.showMessageDialog(null, "Tax: " + nf.format(tax));
JOptionPane.showMessageDialog(null, "Your total amount is " + nf.format(payment));
} catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Invalid Input", "Alert", JOptionPane.ERROR_MESSAGE);
}
}
}
private class CancelListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}
public static void main (String [] args){
BusPayment mycalc = new BusPayment();
}
Geanna Afera 0 Newbie Poster
John_165 44 Junior Poster
I got null pointer exception because you did not initialize frame.
frame = new JFrame();
Edited by John_165
Geanna Afera commented: Oh I see. Is there anything else that is wrong in my code? +0
John_165 44 Junior Poster
Your class is already extend to JFrame. So you don't need to create a new JFrame instance.
public class BusPayment extends JFrame {
private JButton paynow;
private JButton cancel;
private JComboBox<String> jComboBox1;
private JFrame frame;
private JLabel label1, label2, label3, label4;
private JPanel panel1, panel2, panel3;
private JRadioButton destination1;
private JRadioButton destination2;
private JRadioButton destination3;
private JRadioButton destination4;
private JRadioButton destination5;
private JRadioButton destination6;
private JComboBox company;
private JTextField passengerstextfield;
public BusPayment() {
buildPanel();
}
private void buildPanel() {
panel1 = new JPanel();
label1 = new JLabel("Welcome to the Express Bus Ticketing System! ");
panel2 = new JPanel();
label2 = new JLabel("Number of Passenger(s): ");
company = new JComboBox();
passengerstextfield = new JTextField(8);
label3 = new JLabel("Selected Bus Company: ");
final JComboBox<String> comp = new JComboBox<>(new String[]{"Company1", "Company2", "Company3", "Company4"});
panel3 = new JPanel();
label4 = new JLabel("Select a ticket: ");
destination1 = new JRadioButton("Destination 1 RM15");
JRadioButton des1 = new JRadioButton(Double.toString(15));
des1.setSelected(true);
// des1.add(des1);
destination2 = new JRadioButton("Destination 2 RM18");
JRadioButton des2 = new JRadioButton(Double.toString(18));
des2.setSelected(true);
// des2.add(des2);
destination3 = new JRadioButton("Destination 3 RM15");
JRadioButton des3 = new JRadioButton(Double.toString(15));
des3.setSelected(true);
// des3.add(des3);
destination4 = new JRadioButton("Destination 4 RM12");
JRadioButton des4 = new JRadioButton(Double.toString(12));
des4.setSelected(true);
// des4.add(des4);
destination5 = new JRadioButton("Destination 5 RM16");
JRadioButton des5 = new JRadioButton(Double.toString(16));
des5.setSelected(true);
// des5.add(des5);
destination6 = new JRadioButton("Destination 6 RM13");
JRadioButton des6 = new JRadioButton(Double.toString(13));
des6.setSelected(true);
// des6.add(des6);
paynow = new JButton("Proceed");
paynow.addActionListener(new PayNowListener());
cancel = new JButton("Cancel");
cancel.addActionListener(new CancelListener());
panel1.add(label1);
panel2.add(label2);
panel2.add(passengerstextfield);
panel2.add(label3);
panel2.add(company);
panel3.add(destination1);
panel3.add(destination2);
panel3.add(destination3);
panel3.add(destination4);
panel3.add(destination5);
panel3.add(destination6);
panel3.add(paynow);
panel3.add(cancel);
add(panel1);
add(panel2);
add(panel3);
}
private void destination1ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination2ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination3ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination4ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination5ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private void destination6ActionPerformed(ActionEvent e) {
String price = e.getActionCommand();
System.out.println("Selected ticket price = RM " + price);
}
private class PayNowListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
panel3.setBackground(Color.CYAN);
double passengers;
double price;
double tax;
double payment;
DecimalFormat nf = new DecimalFormat("0.00");
try {
passengers = Double.parseDouble(passengerstextfield.getText());
price
= tax = 0.06;//6%
payment = passengers * price * tax + (passengers * price);
JOptionPane.showMessageDialog(null, "Sub-Total: " + nf.format(passengers * price));
JOptionPane.showMessageDialog(null, "Tax: " + nf.format(tax));
JOptionPane.showMessageDialog(null, "Your total amount is " + nf.format(payment));
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Invalid Input", "Alert", JOptionPane.ERROR_MESSAGE);
}
}
}
private class CancelListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
}
public static void main(String[] args) {
JavaApplication5 mycalc = new JavaApplication5();
mycalc.setTitle("Express Bus Ticketing Management System");
mycalc.pack();
mycalc.setLocationRelativeTo(null);
mycalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mycalc.setVisible(true);
}
}
Output
Edited by John_165
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.