Java Solving Text Field Problem in Java GUI Program

  • Thread starter Thread starter DarkAnt
  • Start date Start date
  • Tags Tags
    Field Text
AI Thread Summary
The discussion centers around a user's first Java GUI program encountering a Null Pointer Exception when attempting to retrieve text from JTextField components. The user discovered that the text fields were null despite inputting data. The issue was identified as being caused by the declaration of new local JTextField variables that shadowed the class-level variables. A solution was provided to remove the "JTextField" keyword from the variable declarations in the constructor. After implementing this fix and correcting some spelling errors, the program began functioning correctly. The user expressed gratitude for the assistance received in resolving the issue.
DarkAnt
Messages
195
Reaction score
0
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);
}
}

}
 
Technology news on Phys.org
Don't use toString(), that's not the right method. Try getText().

- Warren
 
I tried that first and it has the same problem. My text fields still are null even when I type text into them.
 
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
 
ok, i did that and I'm still getting null text fields

btw thanks for you help chroot, its really appreciated
 
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
 
ah, I missed one of those. chroot you are a sage. thank you soooo much
 

Similar threads

Replies
2
Views
4K
Replies
6
Views
3K
Replies
1
Views
4K
Replies
9
Views
3K
Replies
3
Views
3K
Replies
10
Views
2K
Replies
5
Views
2K
Replies
3
Views
13K
Back
Top