Hello,
I am trying to learn event-driven programming, so I coded up an entire program from a textbook that is meant to simulate a monthly payment calculator for a loan. The loan amount, interest rate, and duration of the loan are given as inputs to JTextFields, and the payment amount is supposed to be output into a fourth JTextField.
But even though I copied the program line-for-line from the book, it will not work. The GUI looks fine, but when I try to calculate the payment, it gives me a LONG list of errors (which I confess I don't really know how to interpret--I would like to learn). I would really appreciate it if someone could tell me what is wrong with this code so that I have a correct program to learn from. Here is the code, followed by the errors:
public class LoanPayment //a utility class that calculates monthly payment based on three //variables. The math is all based on a standard formula, also copied exactly from book. { public static double getPayment(double amount, double interest, double years) { double payment = amount * ((interest/1200.0)/(1 - Math.pow(1 + interest/1200.0, -years * 12))); return (Math.round(payment * 100))/100.00; //rounds to 2 decimal places } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class LoanCalculator extends JFrame { private JTextField amountField; private JTextField interestField; private JTextField yearsField; private JTextField paymentField; private JButton submitButton; private JButton clearButton; private JButton exitButton; public LoanCalculator() { super("Monthly Payment"); setBounds(0, 0, 250, 200); JPanel panel = new JPanel(); //for text fields and labels //make a label for each text field; add the labels and text fields to the panel JLabel amountLabel = new JLabel(); amountLabel.setFont(new Font("Courier", Font.BOLD, 12)); amountLabel.setText(" Amount:"); amountField = new JTextField(10); panel.add(amountLabel); panel.add(amountField); JLabel interestLabel = new JLabel(); interestLabel.setFont(new Font("Courier", Font.BOLD, 12)); interestLabel.setText("Interest:"); interestField = new JTextField(10); panel.add(interestLabel); panel.add(interestField); JLabel yearsLabel = new JLabel(); yearsLabel.setFont(new Font("Courier", Font.BOLD, 12)); yearsLabel.setText(" Years:"); yearsField = new JTextField(10); panel.add(yearsLabel); panel.add(yearsField); JLabel paymentLabel = new JLabel(); paymentLabel.setFont(new Font("Courier", Font.BOLD, 12)); paymentLabel.setText(" Payment:"); JTextField paymentField = new JTextField(10); panel.add(paymentLabel); panel.add(paymentField); paymentField.setEditable(false); add(panel, BorderLayout.CENTER); //creat three buttons, add them to a new panel, add panel to bottom of frame JPanel buttonPanel = new JPanel(); submitButton = new JButton("Submit"); clearButton = new JButton("Clear"); exitButton = new JButton("Exit"); buttonPanel.add(submitButton); buttonPanel.add(clearButton); buttonPanel.add(exitButton); add(buttonPanel, BorderLayout.SOUTH); //register a listener with each button submitButton.addActionListener(new ButtonListener()); clearButton.addActionListener(new ButtonListener()); exitButton.addActionListener(new ButtonListener()); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == submitButton) try { double amount = Double.parseDouble(amountField.getText()); //possible NumberFormatException double interest = Double.parseDouble(interestField.getText()); double years = Double.parseDouble(yearsField.getText()); double payment = LoanPayment.getPayment(amount, interest, years); //setText() requires a String reference; 'payment + ""' returns a String paymentField.setText(payment + ""); } catch (NumberFormatException excep) { paymentField.setText("Illegal input"); } else if (e.getSource() == clearButton) { amountField.setText(""); interestField.setText(""); yearsField.setText(""); paymentField.setText(""); } else System.exit(0); } } public static void main(String[] args) { LoanCalculator frame = new LoanCalculator(); } }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at LoanCalculator$ButtonListener.actionPerformed(LoanCalculator.java:85) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6288) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6053) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4651) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4481) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4481) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue$2.run(EventQueue.java:616) at java.awt.EventQueue$2.run(EventQueue.java:614) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:613) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Thanks!