?
Solved

Problem adding JTextField to Container

Posted on 2006-04-23
4
Medium Priority
?
202 Views
Last Modified: 2010-03-31
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 );---
0
Comment
Question by:reefcrazed
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 16519680
Try

getContentPane().add( centerLayout,BorderLayout.CENTER );
validate();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16519684
(IOW you may not have assigned 'c' - so check it)
0
 

Author Comment

by:reefcrazed
ID: 16519718
That did it CEHJ, Thanks a ton!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16519724
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question