0

Having problem with my create button method. Compiles/runs ok, but when attempt to calculate, I get the following error: "LoanFrame$1CalcListener.actionPerformed(LoanFrame.java:60)" which is my annualR doubleparse code. Any reasons why this is giving me error, and ways to fix it?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class LoanFrame extends JFrame {



private static final int FRAME_WIDTH = 650;
private static final int FRAME_LENGTH = 300;
private static final double INITIAL_BALANCE = 0.0;
private static final double ANNUAL_RATE = 0.0;
private static final int YEARS = 0;

String[] result;
private JLabel initialLabel;
private JLabel rate;
private JLabel years;
private JTextField initialBal;
private JTextField annualRate;
private JTextField numOfYears;
private JButton calculate;
private JPanel panel;
private JList box;
private JScrollPane scroll;


 LoanFrame(){
    createTextField();
    createButton();
    createScrollPane();
    createPanel();
    setSize(FRAME_WIDTH, FRAME_LENGTH);
}   

private void createTextField(){
    final int FIELD_WIDTH = 10;
    initialLabel = new JLabel("Initial Balance");
    initialBal = new JTextField(FIELD_WIDTH);
    rate = new JLabel("Annual Rate");
    annualRate = new JTextField(FIELD_WIDTH);
    years = new JLabel("Number of Years");
    numOfYears = new JTextField(FIELD_WIDTH);
}
private void createButton(){
      calculate = new JButton("Calculate");
      class CalcListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            double initialB = Double.parseDouble(initialBal.getText());
            double annualR = Double.parseDouble(rate.getText());
            int y = Integer.parseInt(years.getText());
            annualR = annualR / 100;

            for (int i = 0; i < y; i++) {
                double newbalance = initialB * annualR;
                initialB += newbalance;
                String test = String.valueOf(initialB);
                result[i] = test;
            }
            box = new JList(result);
            scroll = new JScrollPane(box);
            getContentPane().add(scroll);
         }            
      }
      ActionListener d = new CalcListener();
      calculate.addActionListener(d);
}
private void createScrollPane(){
    scroll = new JScrollPane();
}
private void createPanel()
   {
      panel = new JPanel();
      panel = new JPanel();
      panel.add(initialLabel);
      panel.add(initialBal);
      panel.add(rate);
      panel.add(annualRate);
      panel.add(years);
      panel.add(numOfYears);
      panel.add(calculate);
      panel.add(scroll);
      add(panel);
   }
}

EDIT: thanks for the replies, my error(After fixing double parse to (annualRate.getText)

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Number of Years"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at LoanFrame$1CalcListener.actionPerformed(LoanFrame.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
4

3 回答 3

4

你正在阅读JLabel而不是阅读JTextField

double annualR = Double.parseDouble(rate.getText());

应该

double annualR = Double.parseDouble(annualRate.getText());
于 2013-11-04T15:06:05.400 回答
1

NullPointerException在这一行得到一个:

for (int i = 0; i < y; i++) {           
    double newbalance = initialB * annualR;
    initialB += newbalance;
    String test = String.valueOf(initialB);
    result[i] = test; // <-- NPE here
}

因为result从未初始化过。

于 2013-11-04T15:07:44.293 回答
0

我收到以下错误:“LoanFrame$1CalcListener.actionPerformed(LoanFrame.java:60)”这是我的 AnnualR doubleparse 代码

的文本rate.getText()格式无效。

于 2013-11-04T15:06:18.653 回答