简体   繁体   中英

NullPointerException at actionPerformed. Not sure why

I am creating a Java program that uses a GUI to display a mortgage payment. I am trying to output to a textArea the payments that will be made for the mortgage. Here is my code:

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

public class MortgageGui extends JFrame implements ActionListener {

// Set two-places for decimal format
DecimalFormat twoPlaces = new DecimalFormat("$0.00");

// Declare variable for calculation
Double loanAmt;
double interestRate;
double monthlyPayment;
int payment;
String amount;

JTextField loanAmount;
JComboBox loanTypeBox;
JLabel paymentOutput;
JTextArea paymentList;

// Build arrays for mortgages
double[] mortgage1 = {7.0, 5.35, 0.0}; // (years, interest, monthly payment)
double[] mortgage2 = {15.0, 5.5, 0.0}; // (years, interest, monthly payment)
double[] mortgage3 = {30.0, 5.75, 0.0}; // (years, interest, monthly payment)


public MortgageGui() {

    super("Mortgage Calculator");
    setLookAndFeel();
    setSize(350, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Loan Amount Panel
    JPanel loanAmtPanel = new JPanel();
    JLabel loanAmtLabel = new JLabel("Loan Amount: ", JLabel.LEFT);
    JTextField loanAmount = new JTextField(10);
    loanAmtPanel.add(loanAmtLabel);
    loanAmtPanel.add(loanAmount);

    // Loan Type Panel
    JPanel loanTypePanel = new JPanel();
    JLabel loanTypeLabel = new JLabel("Loan Type: ", JLabel.LEFT);
    String[] items = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
    JComboBox loanTypeBox = new JComboBox(items);
    loanTypePanel.add(loanTypeLabel);
    loanTypePanel.add(loanTypeBox);

    // Calculate Button Panel
    JPanel calculatePanel = new JPanel();
    JButton calcButton = new JButton("Calculate Paytment");
    calcButton.addActionListener(this);
    calculatePanel.add(calcButton);

    // Monthly Payment Panel
    JPanel paymentPanel = new JPanel();
    JLabel paymentLabel = new JLabel("Monthly Payment: ", JLabel.LEFT);
    JLabel paymentOutput = new JLabel("Calculated Payment");
    paymentPanel.add(paymentLabel);
    paymentPanel.add(paymentOutput);

    // View Payments Panel
    JPanel viewPayments = new JPanel();
    JTextArea paymentList = new JTextArea("", 17, 27);
    paymentList.setEditable(false);
    paymentList.setLineWrap(true);
    viewPayments.add(paymentList);

    // Add panels to win Panel
    JPanel win = new JPanel();
    BoxLayout box = new BoxLayout(win, BoxLayout.Y_AXIS);
    win.setLayout(box);
    win.add(loanAmtPanel);
    win.add(loanTypePanel);
    win.add(calculatePanel);
    win.add(paymentPanel);
    win.add(viewPayments);
    add(win);

    // Make window visible
    setVisible(true);
}


private void setLookAndFeel() {
    try {
        UIManager.setLookAndFeel(
            "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
        );
    } catch (Exception exc) {
        // ignore
      }
}


public void actionPerformed(ActionEvent e) {

    // Clear payment list
    paymentList.setText("");

    // Get loan amount from textfield
    String amount = loanAmount.getText();
    loanAmt = Double.valueOf(amount).doubleValue();

    // Find which mortgate array to use from combobox
    Object obj = loanTypeBox.getSelectedItem();
    String loanSelected = obj.toString();

    // Run the calculation based on the mortgage arrays

    // 7-year loan
    if (loanSelected.equals("7 years at 5.35%")) {
        // Calculate payment, interest, and remaining
        mortgage1[2] = (loanAmt + (loanAmt * (mortgage1[1] / 100))) / (mortgage1[0] * 12);
        double interest1 = (loanAmt * (mortgage1[1] / 100)) / 84;
        double amountRemaining1 = loanAmt + (loanAmt * (mortgage1[1] / 100));

        // Loop through payments
        for (int payment = 1; payment <=84; payment++) {

            // Deduct one payment from the balance
            amountRemaining1 = amountRemaining1 - mortgage1[2];

            // Write payment to textArea
            paymentList.append("Payment " + payment + ": $" + twoPlaces.format(mortgage1[2]) +
                               " / " + "Interest: $" + twoPlaces.format(interest1) + " / " +
                               "Remaining: $" + twoPlaces.format(amountRemaining1) + "\n");
        }

    } else {
        // 15-year loan
        if (loanSelected.equals("15 years at 5.5%")) {

            // Calculate payment, interest, and remaining
            mortgage2[2] = (loanAmt + (loanAmt * (mortgage2[1] / 100))) / (mortgage2[0] * 12);
            double interest2 = (loanAmt * (mortgage2[1] / 100)) / 180;
            double amountRemaining2 = loanAmt + (loanAmt * (mortgage2[1] / 100));

            // Loop through payments
            for (int payment = 1; payment <=180; payment++) {

                // Deduct one payment from the balance
                amountRemaining2 = amountRemaining2 - mortgage2[2];

                // Write payment to textArea
                paymentList.append("Payment " + payment + ": $" + twoPlaces.format(mortgage2[2]) +
                                   " / " + "Interest: $" + twoPlaces.format(interest2) + " / " +
                                   "Remaining: $" + twoPlaces.format(amountRemaining2) + "\n");
            }

        } else {
            //30-year loan

            //Calculate payment, interest, and remaining
            mortgage3[2] = (loanAmt + (loanAmt * (mortgage3[1] / 100))) / (mortgage3[0] * 12);
            double interest3 = (loanAmt * (mortgage3[1] / 100)) / 360;
            double amountRemaining3 = loanAmt + (loanAmt * (mortgage3[1] / 100));

            // Loop through payments
            for (int payment = 1; payment <=360; payment++) {

                // Deduct one payment from the balance
                amountRemaining3 = amountRemaining3 - mortgage3[2];

                // Write payment to textArea
                paymentList.append("Payment " + payment + ": $" + twoPlaces.format(mortgage3[2]) +
                                   " / " + "Interest: $" + twoPlaces.format(interest3) + " / " +
                                   "Remaining: $" + twoPlaces.format(amountRemaining3) + "\n");
            }
        }
    }
}


public static void main(String[] arguments) {
    MortgageGui calc = new MortgageGui();
}
}

When I run the program I see the GUI but when I hit the button to calculate I get this in the console:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at MortgageGui.actionPerformed(MortgageGui.java:100) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Con tainer.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2713) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:680) at java.awt.EventQueue$4.run(EventQueue.java:678) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPriv ilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I cannot figure out where I am going wrong. Please help!

You're redeclaring the paymentList variable in the class's constructor

  JTextArea paymentList = new JTextArea("", 17, 27);

The variable inside of the constructor "shadows" the class field, so that the constructor doesn't see the class field. This local variable is visible only within the constructor (since it was declared inside of it) and the class field will remain null.

Solution: don't re-declare the variable in the constructor. It's OK to initialize it there, but don't re-declare it:

  paymentList = new JTextArea("", 17, 27);

On a more general note, you should always inspect the variables on the line that throws the NPE because one of them is null. If you saw that it was paymentList, you'd quickly be able to identify what I just showed you and you wouldn't need our help, which is kind of the goal of this forum -- to get you to be able to solve things on your own.

Edit:
Also please note that you do this error more than once in your code. I'll let you find the other occurrences of this.

Edit 2:
Consider putting your JTextArea in a JScrollPane. Consider using some decent layout managers and not setting the size of the GUI but rather letting the layout managers do this for you.

The field:

JTextArea paymentList;

is never initialized, so it is null when you try to set the value to "".

The same goes for loanAmount .

@Hovercraft Full Of Eels has explained why they aren't initialised as you think they are, in another answer...

Bonus tip : the line

  loanAmt = Double.valueOf(amount).doubleValue();

will throw an exception if the loan amount is blank (or not a valid double) - you'll need to use a try-catch to handle this

"loanAmount", "loanTypeBox", "paymentList"

For these three, You have to comment out these three lines, because you have already declared these, only assignment needed;

    //JTextField loanAmount = new JTextField(10);       // loanAmount: Already declared, only assignment needed
    loanAmount = new JTextField(10);

    //JComboBox loanTypeBox = new JComboBox(items);     // loanTypeBox: Already declared, only assignment needed
    loanTypeBox = new JComboBox(items);

    //JTextArea paymentList = new JTextArea("", 17, 27);// paymentList: Already declared, only assignment needed
    paymentList = new JTextArea("", 17, 27);

Here is the updated code;

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

public class MortgageGui extends JFrame implements ActionListener {

    // Set two-places for decimal format
    DecimalFormat twoPlaces = new DecimalFormat("$0.00");

    // Declare variable for calculation
    Double loanAmt;
    double interestRate;
    double monthlyPayment;
    int payment;
    String amount;

    JTextField loanAmount;
    JComboBox loanTypeBox;
    JLabel paymentOutput;
    JTextArea paymentList;

    // Build arrays for mortgages
    double[] mortgage1 = { 7.0, 5.35, 0.0 }; // (years, interest, monthly
                                                // payment)
    double[] mortgage2 = { 15.0, 5.5, 0.0 }; // (years, interest, monthly
                                                // payment)
    double[] mortgage3 = { 30.0, 5.75, 0.0 }; // (years, interest, monthly
                                                // payment)

    public MortgageGui() {

        super("Mortgage Calculator");
        setLookAndFeel();
        setSize(350, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Loan Amount Panel
        JPanel loanAmtPanel = new JPanel();
        JLabel loanAmtLabel = new JLabel("Loan Amount: ", JLabel.LEFT);
        // JTextField loanAmount = new JTextField(10); // loanAmount: Already declared, only assignment needed
        loanAmount = new JTextField(10);
        loanAmtPanel.add(loanAmtLabel);
        loanAmtPanel.add(loanAmount);

        // Loan Type Panel
        JPanel loanTypePanel = new JPanel();
        JLabel loanTypeLabel = new JLabel("Loan Type: ", JLabel.LEFT);
        String[] items = { "7 years at 5.35%", "15 years at 5.5%",
                "30 years at 5.75%" };
        // JComboBox loanTypeBox = new JComboBox(items); // loanTypeBox: Already declared, only assignment needed
        loanTypeBox = new JComboBox(items);
        loanTypePanel.add(loanTypeLabel);
        loanTypePanel.add(loanTypeBox);

        // Calculate Button Panel
        JPanel calculatePanel = new JPanel();
        JButton calcButton = new JButton("Calculate Paytment");
        calcButton.addActionListener(this);
        calculatePanel.add(calcButton);

        // Monthly Payment Panel
        JPanel paymentPanel = new JPanel();
        JLabel paymentLabel = new JLabel("Monthly Payment: ", JLabel.LEFT);
        JLabel paymentOutput = new JLabel("Calculated Payment");
        paymentPanel.add(paymentLabel);
        paymentPanel.add(paymentOutput);

        // View Payments Panel
        JPanel viewPayments = new JPanel();
        // JTextArea paymentList = new JTextArea("", 17, 27); // paymentList: Already declared, only assignment needed
        paymentList = new JTextArea("", 17, 27);
        paymentList.setEditable(false);
        paymentList.setLineWrap(true);
        viewPayments.add(paymentList);

        // Add panels to win Panel
        JPanel win = new JPanel();
        BoxLayout box = new BoxLayout(win, BoxLayout.Y_AXIS);
        win.setLayout(box);
        win.add(loanAmtPanel);
        win.add(loanTypePanel);
        win.add(calculatePanel);
        win.add(paymentPanel);
        win.add(viewPayments);
        add(win);

        // Make window visible
        setVisible(true);
    }

    private void setLookAndFeel() {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception exc) {
            // ignore
        }
    }

    public void actionPerformed(ActionEvent e) {

        // Clear payment list
        paymentList.setText("");

        // Get loan amount from textfield
        String amount = loanAmount.getText();
        loanAmt = Double.valueOf(amount).doubleValue();

        // Find which mortgate array to use from combobox
        Object obj = loanTypeBox.getSelectedItem();
        String loanSelected = obj.toString();

        // Run the calculation based on the mortgage arrays

        // 7-year loan
        if (loanSelected.equals("7 years at 5.35%")) {
            // Calculate payment, interest, and remaining
            mortgage1[2] = (loanAmt + (loanAmt * (mortgage1[1] / 100)))
                    / (mortgage1[0] * 12);
            double interest1 = (loanAmt * (mortgage1[1] / 100)) / 84;
            double amountRemaining1 = loanAmt
                    + (loanAmt * (mortgage1[1] / 100));

            // Loop through payments
            for (int payment = 1; payment <= 84; payment++) {

                // Deduct one payment from the balance
                amountRemaining1 = amountRemaining1 - mortgage1[2];

                // Write payment to textArea
                paymentList.append("Payment " + payment + ": $"
                        + twoPlaces.format(mortgage1[2]) + " / "
                        + "Interest: $" + twoPlaces.format(interest1) + " / "
                        + "Remaining: $" + twoPlaces.format(amountRemaining1)
                        + "\n");
            }

        } else {
            // 15-year loan
            if (loanSelected.equals("15 years at 5.5%")) {

                // Calculate payment, interest, and remaining
                mortgage2[2] = (loanAmt + (loanAmt * (mortgage2[1] / 100)))
                        / (mortgage2[0] * 12);
                double interest2 = (loanAmt * (mortgage2[1] / 100)) / 180;
                double amountRemaining2 = loanAmt
                        + (loanAmt * (mortgage2[1] / 100));

                // Loop through payments
                for (int payment = 1; payment <= 180; payment++) {

                    // Deduct one payment from the balance
                    amountRemaining2 = amountRemaining2 - mortgage2[2];

                    // Write payment to textArea
                    paymentList.append("Payment " + payment + ": $"
                            + twoPlaces.format(mortgage2[2]) + " / "
                            + "Interest: $" + twoPlaces.format(interest2)
                            + " / " + "Remaining: $"
                            + twoPlaces.format(amountRemaining2) + "\n");
                }

            } else {
                // 30-year loan

                // Calculate payment, interest, and remaining
                mortgage3[2] = (loanAmt + (loanAmt * (mortgage3[1] / 100)))
                        / (mortgage3[0] * 12);
                double interest3 = (loanAmt * (mortgage3[1] / 100)) / 360;
                double amountRemaining3 = loanAmt
                        + (loanAmt * (mortgage3[1] / 100));

                // Loop through payments
                for (int payment = 1; payment <= 360; payment++) {

                    // Deduct one payment from the balance
                    amountRemaining3 = amountRemaining3 - mortgage3[2];

                    // Write payment to textArea
                    paymentList.append("Payment " + payment + ": $"
                            + twoPlaces.format(mortgage3[2]) + " / "
                            + "Interest: $" + twoPlaces.format(interest3)
                            + " / " + "Remaining: $"
                            + twoPlaces.format(amountRemaining3) + "\n");
                }
            }
        }
    }

    public static void main(String[] arguments) {
        MortgageGui calc = new MortgageGui();
    }
}

And the output is as below with no any errors observed on the console;

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM