Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Text field problem

  1. May 12, 2004 #1
    This is my first java gui program. When I try to get the text from the text fields i get a null pointer exception. I looked at the text fields with the debugger and found that the text fields were null even though i had typed stuff into them. could someone help me out here? btw I know my programming practices are terrible.

    package project_7_5;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2004</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    */

    class Gui extends JFrame implements ActionListener{
    public double dblMoney = 0;
    public int intCompound;
    public String strMoney = "" + dblMoney;
    public String strError = "";
    public String strPrincipal;
    public String strInterest;
    public String strCompound;
    public String strYears;
    JTextField principalField;
    JTextField interestField;
    JTextField compoundField;
    JTextField yearsField;
    JLabel moneyLabel2;
    JLabel errorLabel;
    JButton calculateButton;
    public Gui() {

    super("Bank of Trehan");
    setSize(200,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
    FlowLayout button = new FlowLayout(FlowLayout.CENTER);
    GridLayout grid = new GridLayout(7,1);
    pane.setLayout(grid);
    JPanel row1 = new JPanel();
    JLabel principalLabel = new JLabel("Principal: $", JLabel.RIGHT);
    JTextField principalField = new JTextField(10);
    JPanel row2 = new JPanel();
    JLabel interestLabel = new JLabel("Interest(%): ", JLabel.RIGHT);
    JTextField interstField = new JTextField(3);
    JPanel row3 = new JPanel();
    JLabel compoundLabel = new JLabel("Compound: ", JLabel.RIGHT);
    JTextField compoundField = new JTextField(2);
    JPanel row4 = new JPanel();
    JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
    JTextField yearsField = new JTextField(10);
    JPanel row5 = new JPanel();
    JButton calculateButton = new JButton("Calculate");
    calculateButton.addActionListener(this);
    JPanel row6 = new JPanel();
    JLabel moneyLabel = new JLabel("Money: $", JLabel.RIGHT);
    JLabel moneyLabel2 = new JLabel(strMoney, JLabel.RIGHT);
    JPanel row7 = new JPanel();
    JLabel errorLabel = new JLabel(strError, JLabel.RIGHT);
    row1.setLayout(flow);
    row2.setLayout(flow);
    row3.setLayout(flow);
    row4.setLayout(flow);
    row5.setLayout(button);
    row6.setLayout(flow);
    row7.setLayout(button);
    row1.add(principalLabel);
    row1.add(principalField);
    row2.add(interestLabel);
    row2.add(interstField);
    row3.add(compoundLabel);
    row3.add(compoundField);
    row4.add(yearsLabel);
    row4.add(yearsField);
    row5.add(calculateButton);
    row6.add(moneyLabel);
    row6.add(moneyLabel2);
    row7.add(errorLabel);
    pane.add(row1);
    pane.add(row2);
    pane.add(row3);
    pane.add(row4);
    pane.add(row5);
    pane.add(row6);
    pane.add(row7);
    setContentPane(pane);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent event)
    {
    String command = event.getActionCommand();
    if (command.equals("Calculate"))
    {
    strError = "";
    strPrincipal = principalField.toString();
    strInterest = interestField.toString();
    strCompound = compoundField.toString();
    strYears = yearsField.toString();

    double principal, money, interest, years;
    int compound;

    principal = Integer.parseInt(strPrincipal);
    interest = Integer.parseInt(strInterest);
    compound = Integer.parseInt(strCompound);
    years = Integer.parseInt(strYears);

    interest = interest / 100;

    years = years / compound;
    money = principal;

    for(int i = 1; i <= years; i++)
    {
    money = money * interest + money;
    }
    dblMoney = money;
    strMoney = "" + dblMoney;
    moneyLabel2.setText(strMoney);
    }
    if (command != "Calculate");
    {
    strError = "Unknown Error";
    errorLabel.setText(strError);
    }
    }

    }
     
  2. jcsd
  3. May 13, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Don't use toString(), that's not the right method. Try getText().

    - Warren
     
  4. May 13, 2004 #3
    I tried that first and it has the same problem. My text fields still are null even when I type text into them.
     
  5. May 13, 2004 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Okay, get rid of the "JTextField" in front of the lines " JTextField principalField = new JTextField(10);" and so on.

    You're creating a new local variable that's shadowing the class variable.

    - Warren
     
  6. May 13, 2004 #5
    ok, i did that and i'm still getting null text fields

    btw thanks for you help chroot, its really appreciated
     
  7. May 13, 2004 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I made the changes I suggested on lines 46, 49, 52, 55, 61, and 63, (as well as fixing the spelling errors on lines 49 and 74). The program works fine.

    - Warren
     
  8. May 13, 2004 #7
    ah, I missed one of those. chroot you are a sage. thank you soooo much
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Text field problem
  1. Embedded text (Replies: 4)

  2. Win7 text size bigger (Replies: 3)

Loading...