Solving Text Field Problem in Java GUI Program

  • Java
  • Thread starter DarkAnt
  • Start date
  • Tags
    Field Text
In summary, the conversation discusses a problem with a Java GUI program that results in null text fields even when text has been entered into them. The conversation includes a code snippet for the program and suggestions for fixing the issue, such as using the getText() method instead of toString() and removing "JTextField" from certain lines of code. In the end, the issue is resolved and the program works as intended.
  • #1
DarkAnt
195
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
  • #2
Don't use toString(), that's not the right method. Try getText().

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

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

FAQ: Solving Text Field Problem in Java GUI Program

How can I create a text field in a Java GUI program?

To create a text field in a Java GUI program, you can use the JTextField class. This class allows you to create a single-line text field where users can input text.

How do I get the text input from a text field in a Java GUI program?

To get the text input from a text field, you can use the getText() method of the JTextField class. This method will return a String containing the text entered by the user.

Can I limit the number of characters allowed in a text field in a Java GUI program?

Yes, you can use the setDocument() method of the JTextField class to set a Document that will limit the number of characters allowed in the text field. You can also use the setLimit() method of the PlainDocument class to specify the maximum number of characters allowed.

How can I validate the input from a text field in a Java GUI program?

You can use the DocumentFilter class to validate the input from a text field in a Java GUI program. This class allows you to specify a set of rules that the input must follow, and will prevent invalid input from being entered.

Is it possible to have multiple text fields in a Java GUI program?

Yes, it is possible to have multiple text fields in a Java GUI program. You can create multiple instances of the JTextField class and add them to your GUI layout. Each text field can have its own unique functionality and limitations.

Similar threads

Replies
2
Views
3K
Replies
6
Views
2K
Replies
1
Views
2K
Replies
2
Views
4K
Replies
2
Views
4K
Replies
9
Views
3K
Replies
4
Views
3K
Back
Top