Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't see what is the problem! It worked fine but then I added some variables and now it tells me that
Java
Exception in thread "main" java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1043)
	at java.awt.Container.add(Container.java:363)
	at Expenses.buildPanel(Expenses.java:100)
	at Expenses.<init>(Expenses.java:46)
	at Expenses.main(Expenses.java:108)
Java Result: 1

This is my code, you can just copy and paste it.
Java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


/**
 *
 * @author Alexander
 */
public class Expenses extends JPanel
{
    String stringDay,stringAirfare,stringCarRental,stringMilesDriven,stringParkingFee,stringTaxiCharges,stringSeminarFee,stringLodgingFee;
    double totalAllowed,numberDays,AirFare,CarRental,MilesDriven,ParkingFee,TaxiCharges,seminarFee,lodgingFee;
    private JPanel panel;
    private JLabel numberOfDays;
    private JLabel amountAirfare;
    private JLabel amontRentalFee;
    private JLabel numberMiles;
    private JLabel parkingFee;
    private JLabel taxiFee;
    private JLabel seminarFeeLabel;
    private JLabel lodgingFeeLabel;
    private JLabel totalExpenses;
    private JLabel totalAllowedLabel;
    private JLabel excessMustPaid;
    private JLabel amountSaved;
    
    private JTextField daysField;
    private JTextField amountAirfareField;
    private JTextField rentalFeeField;
    private JTextField milesDrivenField;
    private JTextField parkingFeeField;
    private JTextField taxiFeeField;
    private JTextField smeinarFeeField;
    private JTextField lodgingFeeField;
    
    private JButton calculateButton;
    
    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    
    public Expenses()
    {
        JFrame frame = new JFrame("Travel Expenses");
        frame.setSize(WIDTH,HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        frame.add(panel);
        frame.setVisible(true);
        
    }
    private void buildPanel()
    {
        
     numberOfDays = new JLabel("Number of days on the trip: ");
     daysField = new JTextField(10);
     
     amountAirfare = new JLabel("Amount of airfare: ");
     amountAirfareField = new JTextField(10);
     
     amontRentalFee = new JLabel("Amount of car rental fee: ");
     rentalFeeField = new JTextField(10);
     
     numberMiles = new JLabel("Number of miles driven: ");
     milesDrivenField = new JTextField(10);
     
     parkingFee = new JLabel("Amount of parking fees: ");
     parkingFeeField = new JTextField(10);
     
     taxiFee = new JLabel("Taxi charges: ");
     taxiFeeField = new JTextField(10);
     
     seminarFeeLabel = new JLabel("Conference or seminar registration fees: ");
     smeinarFeeField = new JTextField(10);
     
     lodgingFeeLabel = new JLabel("Lodging charges, per night");
     lodgingFeeField = new JTextField(10);
     
     totalAllowedLabel = new JLabel("Total allowed to spend is "+ totalAllowed);
     calculateButton = new JButton("Calculate");
     
     panel = new JPanel();
     
     panel.add(numberOfDays);
     panel.add(daysField);
     panel.add(amountAirfare);
     panel.add(amountAirfareField);
     panel.add(amontRentalFee);
     panel.add(rentalFeeField);
     panel.add(numberMiles);
     panel.add(milesDrivenField);
     panel.add(parkingFee);
     panel.add(parkingFeeField);
     panel.add(taxiFee);
     panel.add(taxiFeeField);
     panel.add(seminarFeeLabel);
     panel.add(smeinarFeeField);
     panel.add(lodgingFeeLabel);
     panel.add(lodgingFeeField);
     panel.add(calculateButton);
     panel.add(totalExpenses);
     panel.add(totalAllowedLabel);
     panel.add(excessMustPaid);
     panel.add(amountSaved);
    }
    
    public static void main(String[] args)
    {
        new Expenses();
    }
     public void actionPerformed(ActionEvent e)
     {

         final double dayMeal = 37.00;
         final double parkingFee = 10.00;
         final double taxiFee = 20.00;
         final double lodgingCharge = 95.00;
         final double rentCar = 0.27;
         double totalMilesDriven;
         
         stringDay = daysField.getText();
         numberDays = Double.parseDouble(stringDay);
         
         stringAirfare = amountAirfareField.getText();
         AirFare = Double.parseDouble(stringAirfare);
         
         stringCarRental = rentalFeeField.getText();
         CarRental = Double.parseDouble(stringCarRental);
         
         stringMilesDriven = milesDrivenField.getText();
         MilesDriven = Double.parseDouble(stringMilesDriven);
         
         stringParkingFee = parkingFeeField.getText();
         ParkingFee = Double.parseDouble(stringParkingFee);
         
         stringTaxiCharges = taxiFeeField.getText();
         TaxiCharges = Double.parseDouble(stringTaxiCharges);
         
         stringSeminarFee = smeinarFeeField.getText();
         seminarFee = Double.parseDouble(stringSeminarFee);
         
         stringLodgingFee = lodgingFeeField.getText();
         lodgingFee = Double.parseDouble(stringLodgingFee);
         
         totalMilesDriven = MilesDriven * rentCar;
         totalAllowed = numberDays * dayMeal * parkingFee * taxiFee * lodgingCharge * rentCar * totalMilesDriven;</init>
Posted
Updated 29-Mar-12 7:59am
v5

Java
totalAllowed = new JLabel("Total allowed to spend is "+ totalAllowed);

? ? ? ? ? What are you trying to do here?

Peter
 
Share this answer
 
Comments
Alexander Alekseyevich Fedoseev 28-Mar-12 23:36pm    
here I am trying to display "Total allowed to spend is" + whatever is the total of the trip.

Here's the problem


Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide:
• Number of days on the trip
• Amount of airfare, if any
•Amount of car rental fees, if any
• Number of miles driven, if a private vehicle was used
• Amount of parking fees, if any
• Amount of taxi charges, if any
• Conference or seminar registration fees, if any
• Lodging charges, per night
The company reimburses travel expenses according to the following policy:
• $37 per day for meals
• Parking fees, up to $10.00 per day
• Taxi charges up to $20.00 per day
• Lodging charges up to $95.00 per day
• If a private vehicle is used, $0.27 per mile driven
Peter_in_2780 29-Mar-12 0:03am    
Look again at the line I quoted. Read it as fred = new Jlabel("Words" + fred); Where are you getting the RHS fred from?
Alexander Alekseyevich Fedoseev 29-Mar-12 0:07am    
I am getting it from here
totalAllowed = numberDays * dayMeal * parkingFee * taxiFee * lodgingCharge * rentCar * totalMilesDriven;
Alexander Alekseyevich Fedoseev 29-Mar-12 0:10am    
OHHH I SEE,
Alexander Alekseyevich Fedoseev 29-Mar-12 0:20am    
Well wait, now i changed the totalAllowed to totalAllowedLabel , but now i have red squiggly underlines for totalAllowed
totalAllowedLabel = new JLabel("Total allowed to spend is "+ totalAllowed);
Got it to work!



Java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

/**
 *
 * @author Alexander
 */
public class Expenses extends JFrame {

    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    JPanel panel = new JPanel();
    private JLabel numberOfDaysLabel;
    private JLabel amountAirfareLabel;
    private JLabel amontRentalFeeLabel;
    private JLabel numberMilesLabel;
    private JLabel parkingFeeLabel;
    private JLabel taxiFeeLabel;
    private JLabel seminarFeeLabel;
    private JLabel lodgingFeeLabel;
    private JTextField daysField;
    private JTextField amountAirfareField;
    private JTextField rentalFeeField;
    private JTextField milesDrivenField;
    private JTextField parkingFeeField;
    private JTextField taxiFeeField;
    private JTextField smeinarFeeField;
    private JTextField lodgingFeeField;
    private JButton calculateButton;

    public Expenses() {
        setTitle("Travel Expenses");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);

    }

    private void buildPanel() {

        numberOfDaysLabel = new JLabel("Number of days on the trip: ");
        daysField = new JTextField(10);

        amountAirfareLabel = new JLabel("Amount of airfare: ");
        amountAirfareField = new JTextField(10);

        amontRentalFeeLabel = new JLabel("Amount of car rental fee: ");
        rentalFeeField = new JTextField(10);

        numberMilesLabel = new JLabel("Number of miles driven: ");
        milesDrivenField = new JTextField(10);

        parkingFeeLabel = new JLabel("Amount of parking fees: ");
        parkingFeeField = new JTextField(10);

        taxiFeeLabel = new JLabel("Taxi charges: ");
        taxiFeeField = new JTextField(10);

        seminarFeeLabel = new JLabel("Conference or seminar registration fees: ");
        smeinarFeeField = new JTextField(10);

        lodgingFeeLabel = new JLabel("Lodging charges, per night");
        lodgingFeeField = new JTextField(10);

        calculateButton = new JButton("Calculate");
        calculateButton.addActionListener(new buttonListener());
        panel = new JPanel(new GridLayout(9, 2, 5, 10));

        panel.add(numberOfDaysLabel);
        panel.add(daysField);
        panel.add(amountAirfareLabel);
        panel.add(amountAirfareField);
        panel.add(amontRentalFeeLabel);
        panel.add(rentalFeeField);
        panel.add(numberMilesLabel);
        panel.add(milesDrivenField);
        panel.add(parkingFeeLabel);
        panel.add(parkingFeeField);
        panel.add(taxiFeeLabel);
        panel.add(taxiFeeField);
        panel.add(seminarFeeLabel);
        panel.add(smeinarFeeField);
        panel.add(lodgingFeeLabel);
        panel.add(lodgingFeeField);
        panel.add(calculateButton);

    }

    public class buttonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String stringDay, stringAirfare, stringCarRental, stringMilesDriven, stringParkingFee, stringTaxiCharges, stringSeminarFee, stringLodgingFee;
            double totalAllowed, totalOwe, totalSaved, totalExpenses, numberDays, AirFare, CarRental, MilesDriven, ParkingFee, TaxiCharges, seminarFee, lodgingFee;
            final double dayMeal = 37.00;
            final double parkingFee = 10.00;
            final double taxiFee = 20.00;
            final double lodgingCharge = 95.00;
            final double rentCar = 0.27;
            double totalMilesDriven;

            stringDay = daysField.getText();
            numberDays = Double.parseDouble(stringDay);

            stringAirfare = amountAirfareField.getText();
            AirFare = Double.parseDouble(stringAirfare);

            stringCarRental = rentalFeeField.getText();
            CarRental = Double.parseDouble(stringCarRental);

            stringMilesDriven = milesDrivenField.getText();
            MilesDriven = Double.parseDouble(stringMilesDriven);

            stringParkingFee = parkingFeeField.getText();
            ParkingFee = Double.parseDouble(stringParkingFee);

            stringTaxiCharges = taxiFeeField.getText();
            TaxiCharges = Double.parseDouble(stringTaxiCharges);

            stringSeminarFee = smeinarFeeField.getText();
            seminarFee = Double.parseDouble(stringSeminarFee);

            stringLodgingFee = lodgingFeeField.getText();
            lodgingFee = Double.parseDouble(stringLodgingFee);

            totalMilesDriven = MilesDriven * rentCar;
            totalExpenses = AirFare * CarRental * MilesDriven * ParkingFee * TaxiCharges * seminarFee * lodgingFee;
            totalAllowed = numberDays * dayMeal * parkingFee * taxiFee * lodgingCharge * rentCar * totalMilesDriven;

            if (totalExpenses > totalAllowed) {
                totalOwe = totalExpenses - totalAllowed;
            } else {
                totalOwe = 0;
            }

            if (totalAllowed > totalExpenses) {
                totalSaved = totalAllowed - totalExpenses;
            } else {
                totalSaved = 0;
            }


            System.out.println("The total expenses for the trip are " + totalExpenses
                    + " \nThe total allowed to spend is " + totalAllowed + "\nThe total you owe is "
                    + totalOwe + "\nTotal saved is " + totalSaved);

        }
    }
}
 
Share this answer
 
v2
Comments
TorstenH. 30-Mar-12 1:10am    
+5 for keeping the structure. I like that you kept the functional stuff out of the GUI itself.

Is the task accomplished with this? Otherwise you could add Validation of Textfields (check if the entered value is a number).
Using a KeyListener implementation you could check that pretty easy.
Also possible with try/catch around your Double parsing, which will throw an Exception when the value is not a number.
Alexander Alekseyevich Fedoseev 30-Mar-12 2:53am    
The task is accomplished, but we haven't learned how to throw exceptions and validate the textfields, but I would love to learn it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900