CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Simple Problem

  1. #1
    Join Date
    Mar 2003
    Posts
    25

    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!

  2. #2
    Join Date
    Mar 2003
    Posts
    25

    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 . . .

  3. #3
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    how about posting the rest of your code within the code tags
    [ code ] and [/code]

    or even just the constructor and your actionPerformed

  4. #4
    Join Date
    Mar 2003
    Posts
    25
    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;
    	       }
        });
    
        }

  5. #5
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    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.

  6. #6
    Join Date
    Mar 2003
    Posts
    25
    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
  •  





Click Here to Expand Forum to Full Width

Featured