Re: Exception in thread "main" java.lang.NoSuchMethodError
"Don" <dkerusk@twcny.rr.com> wrote in message
news:1179426447.859154.92680@o5g2000hsb.googlegroups.com...
Hi everybody,
I'm working a program assignment for school. I thought my code was
good but it won't run. It compiles successfully but gives me this
error during runtime:
Exception in thread "main" java.lang.NoSuchMethodError
Do you have any advice? My code is below.
Thanks in advance,
Don
<-------------------------------------------------------------------------------------------------------------------------------------------------------
/*
Title: Mortgage Payment Calculator
Programmer: Don
Date: May 14, 2007
Filename: MortPayCalc.java
Purpose: This program calculates mortgage payment amounts.
*/
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
class MortPayCalc2
{
private boolean end = false;
private DecimalFormat newFormat = new DecimalFormat ("$#,###.00");
private double dPrincipal, dAPR, dPayment, dCurrMonthInterest,
dCurrMonthPrincipal, dLoanBalance;
private double dLoanPrincipal = dPrincipal;
private int iTerm, iPayments, iOption;
private String Principal;
private String Table = "";
public void GetPrincipal()
{
Principal = JOptionPane.showInputDialog(null, "Enter Loan
Principal (ex. 20000): ", "Mortgage Payment
Calculator",JOptionPane.QUESTION_MESSAGE);
dPrincipal = Double.parseDouble(Principal);
}
public void GetLoanTerms()
{
Object[] possibilities = {"Loan A", "Loan B", "Loan C"};
String s = (String)JOptionPane.showInputDialog(null,"Please choose
your loan terms:\n",
"Mortgage Payment
Calculator",JOptionPane.PLAIN_MESSAGE,null,possibilities,"Loan A");
int [] Terms = {7, 15, 30};
double [] Rates = {5.35, 5.5, 5.75};
if (s == "Loan A")
{
iTerm = Terms[0];
dAPR = Rates[0];
}
else if (s == "Loan B")
{
iTerm = Terms[1];
dAPR = Rates[1];
}
else
{
iTerm = Terms[2];
dAPR = Rates[2];
}
}
public void CalculatePayment()
{
iPayments = iTerm * 12;
dAPR = dAPR/100/12;
dPayment = (dPrincipal*Math.pow((1+dAPR),iPayments)*dAPR)/
(Math.pow((1+dAPR),iPayments)-1);
}
public void CalculateAmortization()
{
for (int i = 0; i < iPayments; i++)
{
dCurrMonthInterest = dLoanPrincipal * dAPR;
dCurrMonthPrincipal = dPayment - dCurrMonthInterest;
dLoanBalance = dLoanPrincipal - dCurrMonthPrincipal;
dLoanPrincipal = dLoanBalance;
String Row = String.format(newFormat.format(dCurrMonthPrincipal) +
" " + newFormat.format(dCurrMonthInterest) + " " +
newFormat.format(dLoanBalance) + "\n");
Table = Table + Row;
}
}
public void DisplayResults()
{
iOption = JOptionPane.showConfirmDialog(null,
"Loan Principal: " + newFormat.format(dPrincipal) + "\n" +
"Loan Annual Percentage Rate: " + dAPR + "%" + "\n" +
"Loan Term In Years: " + iTerm + " years" + "\n" +
"Monthly Loan Payment Amount: " + newFormat.format(dPayment) +
"\n" +
Table + "\n\n" +
"Recalculate?",
"Mortgage Payment Calculator", JOptionPane.YES_NO_OPTION);
}
public void main(String[] args)
{
while (end == false)
{
GetPrincipal();
GetLoanTerms();
CalculatePayment();
CalculateAmortization();
DisplayResults();
if (iOption == JOptionPane.YES_OPTION)
{
end = false;
}
else
{
System.exit(0);
}
}
}
}
All your methods are instance methods hence you need an "instance" object to
execute them against.
at the start of you main method try creating an instance (or object) of your
MortPayCalc2 class:
MortPayCalc2 mortgageCalculator = new MortPayCalc2();
then when you're executing any of your methods, execute them on this object
e.g.
mortgageCalculator.GetPrincipal();
mortgageCalculator.GetLoanTerms(); etc.
also, it's usual convention to name your methods like this: getPrincipal
i.e. starting with a lowercase letter, and classes with an uppercase letter
(as you have done)
Check out the difference between instance and class (or static) methods,
it's an important concept.