My program compiles but when I fun it I cannot get the city from the JComboBox to show in my JOptionPane. It shows up as "null" and when I click the clear button, nothing happens. I'm not sure how to fix these two problems.

Here is my code:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;


public class TravelerGasolineCalculator extends JFrame implements ActionListener
{
    //declare variables
    int totalMiles, mpg, tankSize, numOfOilChanges, mpgOne = 15;
    double oilChange = 30.00, fuelCost, totalCost, costPerMile, fuel, total, numOfGallons, numOfTanks;
    double superUnleadedCost=3.00, unleadedCost=2.90, leadedCost=2.50, dieselCost=4.00;
    int compactTank=13,midSizeTank=18, luxuryTank=15,suvTank=23;
    String location[] = {null,"New York, NY","Minneapolis, MN", "Boise, ID", "Dallas, TX", "Miami, FL", "Las Vegas, NV"};
    String locationTwo[] = {null,"New York, NY","Minneapolis, MN", "Boise, ID", "Dallas, TX", "Miami, FL", "Las Vegas, NV"};
    String vehicle[] = {null,"Compact", "Mid-Size", "Luxury", "SUV"};
    String fuelPrice[] = {null,"Leaded", "Unleaded", "Super Unleaded", "Diesel"};
    String city, cityTwo;

    //COnstruct a panel for each row
    JPanel firstRow = new JPanel();
    JPanel secondRow = new JPanel();
    JPanel thirdRow = new JPanel();
    JPanel fourthRow = new JPanel();
    JPanel fifthRow = new JPanel();
    JPanel sixthRow = new JPanel();
    JPanel seventhRow = new JPanel();
    JPanel eighthRow = new JPanel();
    JPanel ninthRow = new JPanel();
    JPanel tenthRow = new JPanel();


    //Construct a panel for the fields and button
    JPanel fieldPanelOne = new JPanel();
    JPanel fieldPanelTwo = new JPanel();
    JPanel buttonPanelOne = new JPanel();

    //Construct labels and texts boxes
    JLabel beginningLocationLabel = new JLabel("Beginning Location: ");
        JComboBox cityBox = new JComboBox(location);
    JLabel destinationLabel = new JLabel("Destination: ");
        JComboBox cityBoxTwo = new JComboBox(locationTwo);
    JLabel milesLabel = new JLabel("Trip Distance: ");
        JTextField milesField = new JTextField(10);
    JLabel vehicleSizeLabel = new JLabel("Vehicle Size: ");
        JComboBox vehicleBox = new JComboBox(vehicle);
    JLabel fuelLabel = new JLabel("Fuel Type: ");
        JComboBox fuelBox = new JComboBox(fuelPrice);
    JLabel submitLabel = new JLabel("           Press the Submit Button");
    JLabel milesTwoLabel = new JLabel("Trip Distance: ");
        JTextField milesTwoField = new JTextField(10);
    JLabel milesPerGallonLabel = new JLabel("Vehicle MGP: ");
        JTextField milesPerGallonField = new JTextField(10);
    JLabel costPerGallonLabel = new JLabel("Cost Of Fuel Per Gallon: ");
        JTextField costPerGallonField = new JTextField(10);
    JLabel calcLabel = new JLabel("           Press the Calculate button");


    // Construct button
    JButton calcButton = new JButton("Calculate");
    JButton submitButton = new JButton("Submit");
    JButton clearButton = new JButton("Clear");

    public static void main(String[] args)
    {
        //set the look of the interface
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"The Traveler's Gasoline Calculator could not set the Look and Feel.","Error", JOptionPane.INFORMATION_MESSAGE);
        }

        TravelerGasolineCalculator f = new TravelerGasolineCalculator();
        f.setSize(450,400);
        f.setTitle("Traveler's Gasoline Calculator");
        f.setResizable(true);
        f.setLocation(200,200);
        f.setVisible(true);
    }

    public TravelerGasolineCalculator()
    {
        Container c = getContentPane();
        c.setLayout((new BorderLayout()));
        fieldPanelOne.setLayout(new GridLayout(6,2));
        fieldPanelTwo.setLayout(new GridLayout(4,2));
        FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
            firstRow.setLayout(rowSetup);
            secondRow.setLayout(rowSetup);
            thirdRow.setLayout(rowSetup);
            fourthRow.setLayout(rowSetup);
            fifthRow.setLayout(rowSetup);
            sixthRow.setLayout(rowSetup);
            seventhRow.setLayout(rowSetup);
            eighthRow.setLayout(rowSetup);
            ninthRow.setLayout(rowSetup);
            tenthRow.setLayout(rowSetup);
        buttonPanelOne.setLayout(new FlowLayout(FlowLayout.CENTER));

        //Add Fields to rows
        firstRow.add(beginningLocationLabel);
        firstRow.add(cityBox);

        secondRow.add(destinationLabel);
        secondRow.add(cityBoxTwo);

        thirdRow.add(milesLabel);
        thirdRow.add(milesField);

        fourthRow.add(vehicleSizeLabel);
        fourthRow.add(vehicleBox);

        fifthRow.add(fuelLabel);
        fifthRow.add(fuelBox);

        sixthRow.add(submitLabel);

        seventhRow.add(milesTwoLabel);
        seventhRow.add(milesTwoField);

        eighthRow.add(milesPerGallonLabel);
        eighthRow.add(milesPerGallonField);

        ninthRow.add(costPerGallonLabel);
        ninthRow.add(costPerGallonField);

        tenthRow.add(calcLabel);


        //Adds rows to panel
        fieldPanelOne.add(firstRow);
        fieldPanelOne.add(secondRow);
        fieldPanelOne.add(thirdRow);
        fieldPanelOne.add(fourthRow);
        fieldPanelOne.add(fifthRow);
        fieldPanelOne.add(sixthRow);
        fieldPanelTwo.add(seventhRow);
        fieldPanelTwo.add(eighthRow);
        fieldPanelTwo.add(ninthRow);
        fieldPanelTwo.add(tenthRow);

        //adds button to panel
        buttonPanelOne.add(submitButton);
        buttonPanelOne.add(calcButton);
        buttonPanelOne.add(clearButton);

        //Adds panels to frame
        c.add(fieldPanelOne, BorderLayout.NORTH);
        c.add(fieldPanelTwo, BorderLayout.CENTER);
        c.add(buttonPanelOne, BorderLayout.SOUTH);

        //adds funtion to buttons
        calcButton.addActionListener(this);
        submitButton.addActionListener(this);
        clearButton.addActionListener(this);

        //adds function to dropdown lists
        vehicleBox.addActionListener(this);
        fuelBox.addActionListener(this);
        cityBox.addActionListener(this);
        cityBoxTwo.addActionListener(this);

        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    int answer = JOptionPane. showConfirmDialog(null, "Are you sure you want to exit?", "EXIT",JOptionPane.YES_NO_OPTION);
                    if (answer == JOptionPane.YES_OPTION)
                        System.exit(0);
                }
            }
        );

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == submitButton)
        {
            //Calculates total cost with or without an oil change
            total = Double.parseDouble (milesField.getText());
            numOfGallons = total/mpgOne;
            totalCost = numOfGallons*fuelCost;
            costPerMile = totalCost/total;
            numOfTanks = numOfGallons/tankSize;

            DecimalFormat twoDigit = new DecimalFormat("$#,###.##");
            DecimalFormat oneDigit = new DecimalFormat("###.#");

                    if (total < 3000)
                    {
                        JOptionPane.showMessageDialog(null,"The approximate cost of your trip from " + city + " to " + cityTwo + " is " + twoDigit.format(totalCost) + " and you will use " +oneDigit.format(numOfTanks)+ " tanks of fuel.","Approximate Cost",JOptionPane.INFORMATION_MESSAGE);
                    }

                    else
                    {
                        totalCost = numOfGallons * fuelCost + oilChange;
                        costPerMile = totalCost/total;

                        JOptionPane.showMessageDialog(null,"The approximate cost of your trip from " + city + " to " + cityTwo + " is " + twoDigit.format(totalCost) + " and you will use " +oneDigit.format(numOfTanks)+ " tanks of fuel.","Approximate Cost",JOptionPane.INFORMATION_MESSAGE);
                    }
          }

          if(e.getSource() == calcButton)
          {
              mpg = Integer.parseInt (milesPerGallonField.getText());
              total = Double.parseDouble (milesTwoField.getText());
              fuel = Double.parseDouble (costPerGallonField.getText());
              numOfGallons = total/mpg;
              totalCost = numOfGallons*fuel;
              costPerMile = totalCost/total;

              DecimalFormat twoDigit = new DecimalFormat("$#,###.##");

                    if (total < 3000)
                    {
                        JOptionPane.showMessageDialog(null,"Your approximate cost of the trip is " + twoDigit.format(totalCost) + ".","Approximate Cost",JOptionPane.INFORMATION_MESSAGE);
                    }

                    else
                    {
                        totalCost = numOfGallons * fuelCost + oilChange;
                        costPerMile = totalCost/total;

                        JOptionPane.showMessageDialog(null,"Your total cost of the trip is " + twoDigit.format(totalCost) + ".","Cost With Oil Change", JOptionPane.INFORMATION_MESSAGE);
                    }


          }

            JComboBox cityBox= (JComboBox)e.getSource();
                String city = (String)cityBox.getSelectedItem();
                if(city =="New York, NY")
                {
                    city = city;
                }

                else if(city == "Minneapolis, MN")
                {
                    city = city;
                }
                else if(city == "Boise, ID")
                {
                    city = city;
                }
                else if(city == "Dallas, TX")
                {
                    city = city;
                }
                else if(city == "Miami, FL")
                {
                    city = city;
                }
                else if(city == "Las Vegas, NV")
                {
                    city = city;
                }

                JComboBox cityBoxTwo= (JComboBox)e.getSource();
                String cityTwo = (String)cityBox.getSelectedItem();
                if(cityTwo =="New York, NY")
                {
                    cityTwo = cityTwo;
                }
                else if(cityTwo == "Minneapolis, MN")
                {
                    cityTwo = cityTwo;
                }
                else if(cityTwo == "Boise, ID")
                {
                    cityTwo = cityTwo;
                }
                else if(cityTwo == "Dallas, TX")
                {
                    cityTwo = cityTwo;
                }
                else if(cityTwo == "Miami, FL")
                {
                    cityTwo = cityTwo;
                }
                else if(city == "Las Vegas, NV")
                {
                    cityTwo = "Las Vegas, NV";
                }

            JComboBox vehicleBox= (JComboBox)e.getSource();
                String tank = (String)vehicleBox.getSelectedItem();
                if(tank == "Compact")
                {
                    tankSize = compactTank;
                }
                else if(tank == "Mid-Size")
                {
                    tankSize = midSizeTank;
                }
                else if(tank == "Luxury")
                {
                    tankSize = luxuryTank;
                }
                else if(tank == "SUV")
                {
                    tankSize = suvTank;
                }

            JComboBox fuelBox = (JComboBox)e.getSource();
                String fuelType = (String)fuelBox.getSelectedItem();
                if(fuelType == "Leaded")
                {
                    fuelCost = leadedCost;
                }
                else if(fuelType == "Unleaded")
                {
                    fuelCost = unleadedCost;
                }
                else if(fuelType == "Super Unleaded")
                {
                    fuelCost = superUnleadedCost;
                }
                else if(fuelType == "Diesel")
                {
                    fuelCost = dieselCost;
                }

        if(e.getSource() == clearButton)
        {
            clearFields();
        }
    }

    public void clearFields()
    {
        milesField.setText("");
        costPerGallonField.setText("");
        milesPerGallonField.setText("");
    }
}

Also is there a way to add a .gif image to this?

i think you have to implement one more listner for your class

that is ItemListner

to read events from JComboBoxes instead of adding ActionListioner to those components

check it once by implementing ItemListner to those components

and keep line no 205 to 293 with in ItemListener related methods this is causing the some exceptions
to invoke ClearFeilds() method in the given class

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.