0
私はJava Ready to Program 1.4を使用しています。 JTextFieldに何も入力していないのですが、ボタンをクリックする前に変数を入力しました。次の行のアクションリスナーのボタンをクリックすると、このランタイムエラー(java.lang.NumberFormatException:null)をどのように修正できますか?
public class MortgageCalculator extends JFrame implements ActionListener
{
private String nameText, ageText, incomeText, priceText, downPayText, interestRateText, paymentText, amortText;
public MortgageCalculator()
{ //Frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (new Dimension (300, 250));
frame.setTitle ("Mortgage Calculator");
frame.getContentPane().setLayout (new BorderLayout());
//User Information (Input) - Center Panel
JPanel centerPanel = new JPanel (new GridLayout (16, 1));
//Personal Information
centerPanel.add (new JLabel (" Personal Information"));
centerPanel.add (new JLabel ("Full Name:"));
JTextField nameField = new JTextField();
centerPanel.add (nameField);
centerPanel.add (new JLabel ("Your Age:"));
JTextField ageField = new JTextField();
centerPanel.add (ageField);
centerPanel.add (new JLabel ("Yearly Income:"));
JTextField incomeField = new JTextField();
centerPanel.add (incomeField);
centerPanel.add (new JLabel());
//Mortgage Information
centerPanel.add (new JLabel (" Mortgage Information"));
centerPanel.add (new JLabel ("Price of Property:"));
JTextField priceField = new JTextField();
centerPanel.add (priceField);
centerPanel.add (new JLabel ("Down Payment:"));
JTextField downPayField = new JTextField();
centerPanel.add (downPayField);
centerPanel.add (new JLabel ("Interest Rate (%):"));
JTextField interestRateField = new JTextField();
centerPanel.add (interestRateField);
centerPanel.add (new JLabel ("Amortization Period (Years):"));
frame.getContentPane().add (centerPanel, BorderLayout.CENTER);
//User Options (Input) - South Panel
JPanel southPanel = new JPanel (new GridLayout (8, 3));
//Payment Option
southPanel.add (new JLabel ("Payment:"));
southPanel.add (new JLabel ("Type in 1, 2 or 3"));
southPanel.add (new JLabel());
southPanel.add (new JLabel ("1. Weekly"));
southPanel.add (new JLabel ("2. Biweekly"));
southPanel.add (new JLabel ("3. Monthly"));
JTextField paymentField = new JTextField();
southPanel.add (paymentField);
southPanel.add (new JLabel());
southPanel.add (new JLabel());
//Amortization Period Option
southPanel.add (new JLabel ("Amortization Period:"));
southPanel.add (new JLabel ("Type in 1, 2 or 3"));
southPanel.add (new JLabel());
southPanel.add (new JLabel ("1. 20 years"));
southPanel.add (new JLabel ("2. 25 years"));
southPanel.add (new JLabel ("3. 30 years"));
JTextField amortField = new JTextField();
southPanel.add (amortField);
southPanel.add (new JLabel());
southPanel.add (new JLabel());
//To add a blank line in frame, added three blank JLabels because it is 3 horiontal for grid layout
southPanel.add (new JLabel());
southPanel.add (new JLabel());
southPanel.add (new JLabel());
southPanel.add (new JButton ("Clear Information"));
southPanel.add (new JLabel());
JButton nextButton = new JButton ("Next");
southPanel.add (nextButton);
frame.getContentPane().add (southPanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible (true);
nextButton.addActionListener (this);
String nameText = nameField.getText();
String ageText = ageField.getText();
String incomeText = incomeField.getText();
String priceText = priceField.getText();
String downPayText = downPayField.getText();
String interestRateText = interestRateField.getText();
String paymentText = paymentField.getText();
String amortText = amortField.getText();
}
public static void main (String[] args)
{
MortgageCalculator mainApp = new MortgageCalculator();
}
public void actionPerformed (ActionEvent e)
{
//Gets the personal information from the text fields
String name;
int age, payment, amort;
double income, price, downPay, interestRate;
name = nameText;
age = Integer.parseInt (ageText);
income = Double.parseDouble (incomeText);
//Gets the mortgage information from the text fields
price = Double.parseDouble (priceText);
downPay = Double.parseDouble (interestRateText);
interestRate = Double.parseDouble (interestRateText);
interestRate = interestRate/100;
payment = Integer.parseInt (paymentText);
amort = Integer.parseInt (amortText);
//Output frame
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame2.setSize (new Dimension (300, 250));
frame2.setTitle ("Mortgage Calculator");
frame2.getContentPane().setLayout (new BorderLayout());
JPanel westPanel2 = new JPanel (new GridLayout (3, 1));
JLabel nameLabel = new JLabel ("Name: " + name);
westPanel2.add (nameLabel);
JLabel ageLabel = new JLabel ("Age: " + age);
westPanel2.add (ageLabel);
JLabel incomeLabel = new JLabel ("Yearly Income: $" + income);
westPanel2.add (incomeLabel);
frame2.getContentPane().add (westPanel2, BorderLayout.WEST);
JPanel centerPanel2 = new JPanel (new GridLayout (3, 1));
JLabel priceLabel = new JLabel (" Price of property: $" + price);
centerPanel2.add (priceLabel);
JLabel downPayLabel = new JLabel (" Down Payment: $" + downPay);
centerPanel2.add (downPayLabel);
JLabel interestRateLabel = new JLabel (" Interest Rate: " + income);
centerPanel2.add (interestRateLabel);
frame2.getContentPane().add (centerPanel2, BorderLayout.CENTER);
double interest, numerator, l, denominator, payPerPeriod, initialP, mortgageYearly;
if (payment==1)
{
interest = 1 + interestRate/52;
if (amort==1)
{
l = Math.pow (interest, 20 * 52);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
else if (amort==2)
{
l = Math.pow (interest, 25 * 52);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
else // 3. amort30
{
l = Math.pow (interest, 30 * 52);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
mortgageYearly = payPerPeriod * 52;
}
else if (payment == 2)
{
interest = 1 + interestRate/26;
if (amort==1)
{
l = Math.pow (interest, 20 * 26);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
else if (amort==2)
{
l = Math.pow (interest, 25 * 26);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
else // 3. amort30
{
l = Math.pow (interest, 30 * 26);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
mortgageYearly = payPerPeriod * 26;
}
else //3. monthly payment
{
interest = 1 + interestRate/12;
if (amort==1)
{
l = Math.pow (interest, 20 * 12);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
else if (amort==2)
{
l = Math.pow (interest, 25 * 12);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
else // 2. amort30
{
l = Math.pow (interest, 30 * 12);
numerator = l * interest;
denominator = l - 1;
initialP = price - downPay;
payPerPeriod = (numerator/denominator) * initialP;
}
mortgageYearly = payPerPeriod * 12;
}
JPanel eastPanel2 = new JPanel (new GridLayout (3, 1));
JLabel payTotalLabel = new JLabel (" Payment Total: $" + initialP);
eastPanel2.add (priceLabel);
JLabel payPerPeriodLabel = new JLabel (" Payment per period: $" + payPerPeriod);
eastPanel2.add (payPerPeriodLabel);
frame2.getContentPane().add (eastPanel2, BorderLayout.EAST);
JPanel southPanel2 = new JPanel (new GridLayout (2, 1));
double minimumIncome = mortgageYearly/0.28;
JLabel minimumLabel = new JLabel (" Minimum Yearly Income: $" + minimumIncome);
eastPanel2.add (priceLabel);
if (minimumIncome > income)
{
JLabel qualifiedLabel = new JLabel ("You are not qualified for the mortgage");
eastPanel2.add (qualifiedLabel);
}
else
{
JLabel qualifiedLabel = new JLabel ("You are qualified for the mortgage");
eastPanel2.add (qualifiedLabel);
}
frame2.getContentPane().add (eastPanel2, BorderLayout.EAST);
frame2.pack();
frame2.setVisible (true);
}
}
はJTextFieldのテキストを入力した取得します –