import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.*;
class AMORTIZATION extends JFrame
{
int yearsArray[] = { 7, 15, 30 };
double InterestArray[] = { 5.35, 5.5, 5.75 };
public JLabel MonthlyPaymentLabel;
public JLabel LoanSelectLabel;
public JLabel LoanInterestLabel;
public JTextField LoanAmountText;
public JTextField LoanInterestRate;
public JTextField Duration;
public JTextArea MonthlyPayments;
public JComboBox combo;
public JRadioButton custom;
public JRadioButton normal;
public AMORTIZATION (){
setSize(555, 520);
setTitle("Mortgage Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
MonthlyPaymentLabel = new JLabel("$0.00");
LoanSelectLabel = new JLabel(" Select Loan ");
LoanInterestLabel = new JLabel(" Enter Interest % ");
LoanAmountText = new JTextField("200000.00");
LoanInterestRate = new JTextField("5.5");
Duration = new JTextField("10");
MonthlyPayments = new JTextArea(20, 40);
JPanel panel = new JPanel();
JPanel Lpanel = new JPanel();
JPanel Bpanel = new JPanel();
JPanel Boxpanel = new JPanel();
JPanel Scrpanel = new JPanel();
JPanel Radpanel = new JPanel();
JPanel MPpanel = new JPanel();
JRadioButton custom = new JRadioButton ("Custom", false);
JRadioButton normal = new JRadioButton ("Default", true);
JButton calculateButton = new JButton("Calculate");
JButton clearButton = new JButton("Clear");
MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
JLabel LoanAmount = new JLabel("Loan Amount");
Lpanel.add(LoanAmount);
Lpanel.add(LoanAmountText);
panel.add(LoanInterestLabel);
panel.add(LoanInterestRate);
panel.add(Duration);
Bpanel.add(calculateButton);
Bpanel.add(clearButton);
Boxpanel.add(combo);
MPpanel.add(MonthlyPaymentLabel);
Radpanel.add(custom);
Radpanel.add(normal);
JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);
Scrpanel.add(scrollingArea);
custom.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){hidecustom();}});
normal.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){displaycustom();}});
String loanOptions[] = {"30 years @ 5.75%","15 years @ 5.5%","7 years @ 5.35%"};
combo = new JComboBox(loanOptions);
calculateButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){DetermineInt();}});
clearButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){clearAll();}});
setLayout(new FlowLayout(FlowLayout.LEFT));
getContentPane().add(panel);
getContentPane().add(Radpanel);
getContentPane().add(Lpanel);
getContentPane().add(Bpanel);
getContentPane().add(Boxpanel);
getContentPane().add(Scrpanel);
getContentPane().add(MPpanel);
Duration.setVisible(false);
LoanInterestLabel.setVisible(false);
LoanInterestRate.setVisible(false);
}
public void DetermineInt()
{
int currentNumber = 0;
double initialAmount = Double.parseDouble(LoanAmountText.getText());
double Irate = InterestArray[combo.getSelectedIndex()];
NumberFormat nf = NumberFormat.getCurrencyInstance();
if (custom.isSelected()){
Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
currentNumber = Integer.parseInt(Duration.getText());
}
int cn = currentNumber * 12;
double ir = Irate/12;
double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
MonthlyPaymentLabel.setText("Monthly Payment $" + nf.format(monthly_payment));
int index = 1;
double start = initialAmount;
double end = initialAmount;
double MnthInt;
double totInt = 0.0;
String printString = "";
printString += "#" + index + "\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
for(index = 1; index <= currentNumber * 12; index++){
double Temp = start;
double tempInt = 0.0;
MnthInt = 0.0;
tempInt = (Temp * (1.00 + ir / 12)) - Temp;
Temp = Temp - monthly_payment;
Temp += tempInt;
MnthInt += tempInt;
end = Temp;
totInt = totInt + tempInt;
printString += "" + index + "\t" + nf.format(start) + "\t" + nf.format(end) + "\t" + nf.format(MnthInt) + "\t" + nf.format(totInt) + "\n";
start = end;
}
MonthlyPayments.setText(printString);
}
public void displaycustom(){
combo.setVisible(false);
custom.setSelected(true);
normal.setSelected(false);
LoanSelectLabel.setText("Enter Duration (years)");
LoanInterestLabel.setVisible(true);
Duration.setVisible(true);
Duration.setColumns(15);
LoanInterestRate.setVisible(true);
LoanInterestRate.setColumns(15);
}
public void hidecustom(){
combo.setVisible(true);
custom.setSelected(false);
normal.setSelected(true);
LoanSelectLabel.setText("Select Loan");
LoanInterestLabel.setVisible(false);
Duration.setVisible(false);
Duration.setColumns(15);
LoanInterestRate.setVisible(false);
LoanInterestRate.setColumns(15);
}
public void clearAll(){
MonthlyPayments.setText("");
MonthlyPaymentLabel.setText("$0.00");
LoanAmountText.setText("$200000.00");
Duration.setText("10");
LoanInterestRate.setText("5.35");
combo.setSelectedIndex(1);
hidecustom();
}
public static void main (String [] args)throws IOException
{ AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
MyAMORTIZATION.setVisible (true);
}
}
This shows the error message I recieve.
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at AMORTIZATION.<init>(AMORTIZATION.java:91)
at AMORTIZATION.main(AMORTIZATION.java:138)

New Topic/Question
Reply




MultiQuote







|