-
March 19th, 2003, 07:45 PM
#1
Simple Problem
I'm a bit new to Java, and there's one thing I don't understand:
A part of my constructor looks like this:
storeInput.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
// code with an array textfield
}
});
storeInput is a button--I have an array of a class inside the actionPerformed, but it always seems to throw a NullPointerException. I have all objects instantiated in the constructor, and I'm at a loss.
Any help would be greatly appreciated!
-
March 19th, 2003, 08:10 PM
#2
a bit of a clarification
i think i've got the problem down to one thing:
how do you use textField.getText() within actionPerformed? something seems to be giving me a NullPointerException . . .
-
March 19th, 2003, 08:54 PM
#3
how about posting the rest of your code within the code tags
[ code ] and [/code]
or even just the constructor and your actionPerformed
-
March 19th, 2003, 08:59 PM
#4
Thanks a lot! You can scroll down to the actionPerformed ( I think the problem is either immediately hereafter, in my initializing, or in the actionPerformed ).
Code:
public class UserInput extends JPanel implements ActionListener
{
JTextField first, last, initial, phone;
JButton storeInput;
JLabel firstL, lastL, initialL, phoneL, test1, test2, test3, test4;
GridBagLayout gridBag1;
GridBagConstraints constraint1;
int checkingVariable = 0;
PersonRecord data[] = new PersonRecord[49];
public UserInput ()
{
gridBag1 = new GridBagLayout ();
constraint1 = new GridBagConstraints ();
first = new JTextField("Test");
last = new JTextField("Test");
initial = new JTextField("Test");
phone = new JTextField("Test");
setLayout (gridBag1);
test1 = new JLabel("1");
test2 = new JLabel("2");
test3 = new JLabel("3");
test4 = new JLabel("4");
constraint1.fill = GridBagConstraints.BOTH;
constraint1.weightx = 1.0;
constraint1.weighty = 1.0;
constraint1.gridheight = 1;
firstName ();
constraint1.gridwidth = GridBagConstraints.RELATIVE;
constraint1.ipadx = 0;
initialName ();
constraint1.gridwidth = GridBagConstraints.RELATIVE;
constraint1.ipadx = 0;
lastName ();
constraint1.gridwidth = GridBagConstraints.RELATIVE;
constraint1.ipadx = 0;
phoneNumber ();
constraint1.gridwidth = GridBagConstraints.REMAINDER;
storeInput = new JButton ("Click me to store input");
constraint1.ipady = 100;
gridBag1.setConstraints (storeInput, constraint1);
add (storeInput);
storeInput.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
// data[checkingVariable].setFirst(first.getText());
// data[checkingVariable].setInitial(initial.getText());
// data[checkingVariable].setLast(last.getText());
// data[checkingVariable].setPhone(phone.getText());
// test1.setText(data[checkingVariable].getFirstName());
// test2.setText(data[checkingVariable].getInitial());
// test3.setText(data[checkingVariable].getLastName());
// test4.setText(data[checkingVariable].getPhoneNumber());
add(test1);
add(test2);
add(test3);
add(test4);
checkingVariable = checkingVariable + 1;
}
});
}
-
March 19th, 2003, 09:31 PM
#5
I think your NullPointerException has to do with data[]. You've declaried it, but not initialized it. If the code that you remarked out is not supposed to be. This would go in your constructor.
ie
Code:
for(int i = 0; i < data.length(); i++)
data[i] = new PersonRecord();
since you are adding to the panel in actionPreformed, you might want to use UserInput.this.updateUI() in your actionPreformed method. Also, you don't need to implement ActionListener if you are going to override actionPerformed(...) in the way that you are doing it in your constructor.
-
March 19th, 2003, 09:41 PM
#6
Thank you so much ! Works like a charm
I really appreciate your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|