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!