Solving Text Field Problem in Java GUI Program

  • Context: Java 
  • Thread starter Thread starter DarkAnt
  • Start date Start date
  • Tags Tags
    Field Text
Click For Summary

Discussion Overview

The discussion revolves around a Java GUI programming issue where a user encounters a null pointer exception when trying to retrieve text from text fields. The scope includes debugging practices, programming techniques, and potential solutions to the problem.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster experiences a null pointer exception when accessing text fields, despite having entered text into them.
  • One participant suggests using the getText() method instead of toString() to retrieve the text from the fields.
  • The original poster reports that using getText() does not resolve the issue, as the text fields remain null.
  • Another participant advises removing the "JTextField" declaration from the field initializations to avoid shadowing the class variables.
  • The original poster acknowledges the advice but continues to face the same issue with null text fields.
  • A participant confirms that after making the suggested changes and correcting spelling errors, the program functions correctly.
  • The original poster expresses gratitude for the assistance received from participants.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the null pointer exception, as the original poster continues to experience issues despite following suggestions. Multiple viewpoints on the problem and its solutions are presented.

Contextual Notes

The discussion highlights potential limitations in the original code, such as variable shadowing and method usage, but does not resolve the underlying issue of the null pointer exception.

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 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
13K