Problem adding JTextField to Container

Posted on 2006-04-23
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";
                        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);
                  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 );---
Question by:reefcrazed
    LVL 86

    Accepted Solution


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

    Expert Comment

    (IOW you may not have assigned 'c' - so check it)

    Author Comment

    That did it CEHJ, Thanks a ton!
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now