Link to home
Start Free TrialLog in
Avatar of reefcrazed
reefcrazed

asked on

Problem adding JTextField to Container

Okay Experts have another java problem.

I have a program that creates textfields that will be filled with user inputs once they press a "New Users" button. This I have managed to accomplish, but I am also supposed to create a field under the buttons that contains the same infomation. The Problem I am having is creating that field and filling it with the data.

Here is the snippet of code from the actionlistener for that button.
      public void actionPerformed ( ActionEvent event )
      {
            if ( event.getSource() == btnNewUser )
            {
                  JPanel centerLayout = new JPanel();
                  // Prompt User for Input
                  userName = JOptionPane.showInputDialog( "Please Enter Your Name." );
                  userAge = Integer.parseInt(JOptionPane.showInputDialog( "Please Enter Your Age.") );
                  
                  // Determine User Gender
                  userSex = JOptionPane.showInputDialog( "Please Enter you Gender.\nM for Male\nF for Female");
                  if (userSex == "F")
                  {
                        userGender = "Female";
                  }
                  else
                  {
                        userGender = "Male";
                  }
                  
                  userHeight = Integer.parseInt( JOptionPane.showInputDialog( "Please Enter Your Height" +
                              "\nIn Inches") );
                  userWeight = Integer.parseInt( JOptionPane.showInputDialog( "Please Enter Your Weight" +
                              "\n In Pounds") );
                  
                  // Set Text In TextFields
                  nameField.setText( userName );
                  ageField.setText( "" + userAge );
                  genderField.setText( userGender );
                  weightField.setText( "" + userWeight );
                  heightField.setText( "" + userHeight );
                  
                        // Create User Object
                  User user = new User( userName, userAge, userWeight, userSex, userHeight );
                  
                  String displayOutput = userName
                        
                  JTextField display = new JTextField (displayOutput);
                  centerLayout.add(display);
                  c.add( centerLayout,BorderLayout.CENTER );
                  btnOldUser.setEnabled( false );
            }

When I run the program and click the "New User" button it gives a null pointer exception at                    ---c.add( centerLayout,BorderLayout.CENTER );---
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(IOW you may not have assigned 'c' - so check it)
Avatar of reefcrazed
reefcrazed

ASKER

That did it CEHJ, Thanks a ton!
:-)