0

I'm using JCreator Pro. I'm trying to get information entered in the textfields to display and calculate the selling price of used cars. The window looks like this:

enter image description here

When I click display, I want the information to appear on the bottom half of the window (car make and model, year of manufacturing, sale price) But when I do click it, all I get are these lines of text in the General output window and I can't make out what's wrong. My code compiles without any errors.

Here's my main class for reference:

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

public class MIT141674CarDetailsTest extends JFrame implements ActionListener {
    MIT141674Car car;

    JTextField jtfMakeModel, jtfYear, jtfPurchPrice;

    JButton jbtnDisplay, jbtnClear;

    JLabel jlblMakeModel, jlblYear, jlblSellPrice;

    public MIT141674CarDetailsTest() {
        setSize(500, 210);
        setLocationRelativeTo(null);
        setTitle("Purchased car details");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        setLayout(new GridLayout(7, 2));

        // input
        add(new JLabel(" Enter car make and model: "));
        jtfMakeModel = new JTextField();
        add(jtfMakeModel);

        add(new JLabel(" Enter year of manufacturing: "));
        jtfYear = new JTextField();
        add(jtfYear);

        add(new JLabel(" Enter purchase price: "));
        jtfPurchPrice = new JTextField();
        add(jtfPurchPrice);

        // buttons
        jbtnDisplay = new JButton("Display");
        add(jbtnDisplay);
        jbtnDisplay.addActionListener(this);

        jbtnClear = new JButton("Clear all");
        add(jbtnClear);
        jbtnClear.addActionListener(this);

        // display car
        add(new JLabel(" Car make and model: "));
        jlblMakeModel = new JLabel("");
        add(jlblMakeModel);

        add(new JLabel(" Year of manufacturing: "));
        jlblYear = new JLabel("0000");
        add(jlblYear);

        add(new JLabel(" Sale price: "));
        jlblSellPrice = new JLabel("$0.00");
        add(jlblSellPrice);
    }

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

    public void actionPerformed(ActionEvent ae) {
        String str = ae.getActionCommand();

        if (str.equals("Display")) {
            int carYear = Integer.parseInt(jtfYear.getText());
            if (carYear >= 2009) {

                double pPrice = Double.parseDouble(jtfPurchPrice.getText());
                if (pPrice > 0) {

                    car = new MIT141674Car(jtfMakeModel.getText(),
                            carYear, pPrice);

                    jlblMakeModel.setText(car.getMakeModel());
                    jlblYear.setText(Integer.toString(car.getYear()));
                    jlblSellPrice
                            .setText(Double.toString(car.getSellingPrice()));

                } else
                    // pPrice <=0 - invalid
                    JOptionPane.showMessageDialog(null,
                            "Invalid purchase price, please re-enter");
            } // carYear <=2009
            else
                // invalid carYear
                JOptionPane.showMessageDialog(null,
                        "Invalid year of manufacturing, please re-enter");
        } // if display
        else
        // not Display button, then check if it's Clear all button
        if (str.equals("Clear all")) {
            // remove text from all text fields
            jtfMakeModel.setText("");
            jtfYear.setText("");
            jtfPurchPrice.setText("");

            // clear labels
            jlblMakeModel.setText("");
            jlblYear.setText("0000");
            jlblSellPrice.setText("$0.00");
        }
    } // actionPerformed
} // end of class

Can anyone help me and tell me what is wrong?

EDIT: I have partially solved my problem by doing what @Exbury mentioned by changing

    car = new MIT141674Car (jtfMakeModel.getText(), car.getYear(), car.getSellingPrice());

to

    car = new MIT141674Car (jtfMakeModel.getText(), carYear, pPrice);

But now I've found that the year entered only displays if I enter 2009, any other years entered after 2009 comes up as 0.

  • *"all I get are these lines of text in the General output window"* 1) Always copy/paste error and exception output! 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Jun 09 '15 at 05:06
  • Scratch point 2 and replace it with: See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) Also, don't post images of a stack trace, post the ***text of it.*** The text is not only less bytes, but also search engine friendly. – Andrew Thompson Jun 09 '15 at 05:14
  • BTW - line 88 of that source is `else` and I cannot see how it has any possibility of throwing an NPE! It is important to ensure the stack trace refers to the actual piece of code shown to others, those line numbers in the trace are important.. – Andrew Thompson Jun 09 '15 at 05:17
  • Whats at line no. 88? –  Jun 09 '15 at 05:18
  • car = new MIT141674Car (jtfMakeModel.getText(), car.getYear(), car.getSellingPrice()); – Bruce Jun 09 '15 at 05:18
  • @AndrewThompson Thanks for the feedback, It's my first post here so I have a lot to learn. Also I've only just started Intro to Programming so everything is still quite new to me. – Bernard Vierneza Jun 09 '15 at 06:30

2 Answers2

0

Giving one class a getter/accessor method that extracts the desired information of JTextField and giving the other class a setter/mutator method that allows outside objects to inject the desired information, here to set the text of its JLabel

0

While creating car object you are using same car reference (null) in constructor Change this to

car = new MIT141674Car (jtfMakeModel.getText(), car.getYear(), car.getSellingPrice());`

to

car = new MIT141674Car (jtfMakeModel.getText(), carYear, pPrice);
Bruce
  • 8,609
  • 8
  • 54
  • 83
  • Thank you! That sort of solved my problem. The information displays, except the year. Not sure why. Check it out: http://i.imgur.com/miwVfVV.png – Bernard Vierneza Jun 09 '15 at 06:27
  • did you check the set and get methods in your MIT141674Car class – Bruce Jun 09 '15 at 06:55
  • Ah yes, my code had to be so that it didn't accept cars manufactured before 2009 and my 'if' had if(newYear<=2009) instead of if(newYear>=2009) – Bernard Vierneza Jun 09 '15 at 07:15