From 80e706e2240274441de0c471d105548ce5626bcf Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 5 Oct 2009 00:56:57 +0000 Subject: [PATCH] fixed a few null pointer exceptions git-svn-id: svn://anubis/gvsu@441 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs623/proj1/RetirementCalculator.java | 50 ++++++++++++++++++---- cs623/proj1/RetirementCalculatorModel.java | 1 + 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/cs623/proj1/RetirementCalculator.java b/cs623/proj1/RetirementCalculator.java index b71fcfe..46e286a 100644 --- a/cs623/proj1/RetirementCalculator.java +++ b/cs623/proj1/RetirementCalculator.java @@ -19,11 +19,13 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener private JButton myCalculateButton; private JButton myPrintButton; private JButton myResetButton; + private JLabel myResultBox; public RetirementCalculatorPanel() { super(new BorderLayout()); + myModel = new RetirementCalculatorModel(); myFields = new HashMap(); JPanel inner = new JPanel(new GridBagLayout()); class ComboChoice @@ -67,7 +69,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener c.gridy++; c.weightx = 0.0; - inner.add(new JLabel("What is your current annual income?", SwingConstants.RIGHT), c); + inner.add(new JLabel("What is your current annual income?", + SwingConstants.RIGHT), c); c.gridx++; c.weightx = 1.0; field = new JTextField(); @@ -84,7 +87,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener c.gridy++; c.weightx = 0.0; - inner.add(new JLabel(" - from an employer pension plan?", SwingConstants.RIGHT), c); + inner.add(new JLabel(" - from an employer pension plan?", + SwingConstants.RIGHT), c); c.gridx++; c.weightx = 1.0; field = new JTextField(); @@ -94,7 +98,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener c.gridy++; c.weightx = 0.0; - inner.add(new JLabel(" - from a part-time job?", SwingConstants.RIGHT), c); + inner.add(new JLabel(" - from a part-time job?", + SwingConstants.RIGHT), c); c.gridx++; c.weightx = 1.0; field = new JTextField(); @@ -119,7 +124,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener c.gridy++; c.weightx = 0.0; - inner.add(new JLabel("How much savings do you have to date?", SwingConstants.RIGHT), c); + inner.add(new JLabel("How much savings do you have to date?", + SwingConstants.RIGHT), c); c.gridx++; c.weightx = 1.0; field = new JTextField(); @@ -129,7 +135,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener c.gridy++; c.weightx = 0.0; - inner.add(new JLabel("At what age do you expect to retire?", SwingConstants.RIGHT), c); + inner.add(new JLabel("At what age do you expect to retire?", + SwingConstants.RIGHT), c); c.gridx++; c.weightx = 1.0; ComboChoice[] retirement_age_choices = { @@ -146,7 +153,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener JPanel p = new JPanel(new BorderLayout()); p.setAlignmentX(Component.RIGHT_ALIGNMENT); - p.add(new JLabel("What is your life expectancy?", SwingConstants.RIGHT), BorderLayout.CENTER); + p.add(new JLabel("What is your life expectancy?", + SwingConstants.RIGHT), BorderLayout.CENTER); ComboChoice[] life_expectancy_choices = { new ComboChoice(82, "82 (Male; 50th percentile)"), new ComboChoice(86, "86 (Female; 50th percentile)"), @@ -188,6 +196,12 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener c.gridwidth = 1; c.gridy++; + c.gridwidth = 2; + myResultBox = new JLabel(" "); + inner.add(myResultBox, c); + c.gridwidth = 1; + c.gridy++; + JScrollPane scrollpane = new JScrollPane(inner, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); @@ -196,9 +210,27 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener public void actionPerformed(ActionEvent e) { - System.out.println("Event:"); - System.out.println(e.getActionCommand()); - System.out.println(e.paramString()); + if (e.getSource() == myCalculateButton) + calculate(); + else if (e.getSource() == myResetButton) + reset(); + else if (e.getSource() == myPrintButton) + print(); + } + + private void calculate() + { + myModel.calculate(); + } + + private void reset() + { + myModel.reset(); + } + + private void print() + { + calculate(); } } diff --git a/cs623/proj1/RetirementCalculatorModel.java b/cs623/proj1/RetirementCalculatorModel.java index 4e3772d..b795ca1 100644 --- a/cs623/proj1/RetirementCalculatorModel.java +++ b/cs623/proj1/RetirementCalculatorModel.java @@ -8,6 +8,7 @@ public class RetirementCalculatorModel public RetirementCalculatorModel() { + myFields = new HashMap(); reset(); }