Welcome to Dream.In.Code
Become a Java Expert!

Join 150,140 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,293 people online right now. Registration is fast and FREE... Join Now!




Help! Don't know what the problem is!

 
Closed TopicStart new topic

Help! Don't know what the problem is!, problem with a Swing app

freshoreo
4 Aug, 2008 - 03:25 PM
Post #1

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 11

I don't know what I;m doing wrong. It compiles fine but when I run and hit the "Calculate" button, nothing is displayed in TextArea and I get an error. ????

CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class ModifiedFutureValueApp
{
    public static void main(String[] args)
    {
        JFrame frame = new FutureValueFrame();
        frame.setVisible(true);
    }
}

class FutureValueFrame extends JFrame
{
    public FutureValueFrame()
    {
        setTitle("Future Value Calculator");
        centerWindow(this);
        setSize(400, 400);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new FutureValuePanel();
        getContentPane().add(panel);
    }

    private void centerWindow(Window w)
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    }
}

class FutureValuePanel extends JPanel implements ActionListener
{
    private JTextField  paymentTextField,
                        rateTextField;
     private JTextArea   futureValueTextArea;
     private JScrollPane scrollPane;
    private JLabel      paymentLabel,
                        rateLabel,
                        yearsLabel,
                        futureValueLabel;
    private JButton     calculateButton,
                        exitButton;
     private JComboBox   yearsComboBox;

    public FutureValuePanel()
    {
        setLayout(new GridBagLayout());

        // payment label
        paymentLabel = new JLabel("Monthly Payment:");
        add(paymentLabel, getConstraints(0,0,1,1, GridBagConstraints.EAST));

        // payment text field
        paymentTextField = new JTextField(15);
        add(paymentTextField, getConstraints(1,0,1,1, GridBagConstraints.WEST));

        // rate label
        rateLabel = new JLabel("Yearly Interest Rate:");
        add(rateLabel, getConstraints(0,1,1,1, GridBagConstraints.EAST));

        // rate text field
        rateTextField = new JTextField(15);
        add(rateTextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));

        // years label
          yearsLabel = new JLabel("Number of Years:");
        add(yearsLabel, getConstraints(0,2,1,1, GridBagConstraints.EAST));
          
          //years combo box
          String[] years = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
          "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
        yearsComboBox = new JComboBox(years);
          yearsComboBox.setSelectedIndex(0);
          yearsComboBox.addActionListener(this);
        add(yearsComboBox, getConstraints(1,2,1,1, GridBagConstraints.WEST));


        // future value label
        futureValueLabel = new JLabel("Future Value:");
        add(futureValueLabel, getConstraints(0,3,1,1, GridBagConstraints.EAST));

        // future value text field
          JTextArea futureValueTextArea = new JTextArea(5, 15);
          JScrollPane scrollPane = new JScrollPane(futureValueTextArea,
                  ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          futureValueTextArea.setEditable(false);
        add(scrollPane, getConstraints(1,3,1,1, GridBagConstraints.WEST));

        // calculate button
        calculateButton = new JButton("Calculate");
        calculateButton.addActionListener(this);
        add(calculateButton, getConstraints(1,4,1,1, GridBagConstraints.CENTER));

        // exit button
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        add(exitButton, getConstraints(2,4,1,1, GridBagConstraints.WEST));
          
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == exitButton)
            System.exit(0);
        else if (source == calculateButton)
        {
            double payment = Double.parseDouble(paymentTextField.getText());
            double rate = Double.parseDouble(rateTextField.getText());
                int years = yearsComboBox.getSelectedIndex();
            double futureValue = FinancialCalculations.calculateFutureValue(
                payment, rate, years);
            NumberFormat currency = NumberFormat.getCurrencyInstance();
                String result = currency.format(futureValue);
                futureValueTextArea.setText(result);
        }
    }
     private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth
     , int gridheight, int anchor)
     {
         GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.ipadx = 0;
        c.ipady = 0;
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
        c.anchor = anchor;
        return c;
     }
}


Thank you!
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help! Don't Know What The Problem Is!
4 Aug, 2008 - 03:59 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Here is the culprit

CODE

          JTextArea futureValueTextArea = new JTextArea(5, 15);


by doing that you create a temporary JTextArea futureValueTextArea that disappear as soon as uou leave the constructor

when you do

CODE

               futureValueTextArea.setText(result);


in your ActionListener you reference the instance variable futureValueTextArea that hasn;t been initialized

CODE

          futureValueTextArea = new JTextArea(5, 15);   // access the real one


should fix your problem



User is offlineProfile CardPM
+Quote Post

freshoreo
RE: Help! Don't Know What The Problem Is!
4 Aug, 2008 - 04:23 PM
Post #3

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 11

QUOTE(pbl @ 4 Aug, 2008 - 04:59 PM) *

Here is the culprit

CODE

          JTextArea futureValueTextArea = new JTextArea(5, 15);


by doing that you create a temporary JTextArea futureValueTextArea that disappear as soon as uou leave the constructor

when you do

CODE

               futureValueTextArea.setText(result);


in your ActionListener you reference the instance variable futureValueTextArea that hasn;t been initialized

CODE

          futureValueTextArea = new JTextArea(5, 15);   // access the real one


should fix your problem



Thank you!!! That was a tricky one
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help! Don't Know What The Problem Is!
4 Aug, 2008 - 04:53 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(freshoreo @ 4 Aug, 2008 - 05:23 PM) *

Thank you!!! That was a tricky one

Would have been a lot easier if you had told that your error was a null pointer exception

When you have an error please post it

Topic can be closed

This post has been edited by pbl: 4 Aug, 2008 - 08:07 PM
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/9/09 02:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month