Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:09:22+00:00 2026-05-28T18:09:22+00:00

I am creating a Java program that uses a GUI to display a mortgage

  • 0

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(Container.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.doIntersectionPrivilege(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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:09:24+00:00Added an answer on May 28, 2026 at 6:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a program that will download a .jar (java) file from a
I'm creating a java program that creates a file and fills it with data
I'm a java beginner, & I'm creating a program that draws lines using the
I am in the process of creating a Java program that goes on the
I am creating a program that uses JFrame, JPanel, JLabel and all other sorts
I am creating a java application that connects to multiple databases. A user will
I'm trying to write a java program that reads data from an image and
WRITTEN IN JAVA Im creating a program that connects to a proxy and then
When creating applications (Java, run on a normal computer). How important is program size
I've creating a Java Swing application and I realized that I have many many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.