Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6692727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:54:03+00:00 2026-05-26T05:54:03+00:00

I am new to Java and I am trying to allow a user to

  • 0

I am new to Java and I am trying to allow a user to enter an employees first and last name via the Gui and when they press the submit button it activates the listener methods and allows the values entered to be gathered and put in the systems memory

My issue is that when I enter the first name it works perfectly but when I enter the last name it does not work at all I press the submit button and the the whole thing goes nuts the error is null pointer exception “AWT event queue”. And I can see no reason for this happening PLS Help

This is the code the error occurs at line

lName = employeeDetails2.getText();  (located closer to the end of the code)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class guiEmployee1 extends JFrame
{

    private String  fName;
    private String  lName;
    private String  gender;
    private String  payLevel;
    private String  empIDnumber;
//  private int dPayLevel; 

    JTextField employeeDetails1;
    JTextField employeeDetails2;
    JTextField employeeDetails3;    
    JTextField employeeDetails4;
    JTextField employeeDetails5;

    public guiEmployee1()
    {
        JButton submit;
        JButton b1;

        System.out.println("cabanas");

        JFrame frame = new JFrame();
        employeeDetails1 = new JTextField(10);

        JTextField employeeDetails2;
        employeeDetails2 = new JTextField(10);




        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(320, 75));
        frame.setTitle("Employee Details");

        frame.setLayout(new FlowLayout());

        frame.add(new JLabel("Please enter Employees first Name: "));
        frame.add(employeeDetails1);
        ButtonListenerFirstName listener = new ButtonListenerFirstName();

        frame.add(new JLabel("Please enter Employees Last Name: "));
        frame.add(employeeDetails2);
        ButtonListenerLastName listener1 = new  ButtonListenerLastName();




        b1  = new JButton  ("Submit");

        b1.addActionListener(listener);
        b1.addActionListener(listener1);


        frame.add(b1);
        frame.pack();
        frame.setSize(300,300);
        frame.setVisible(true);



    }


     public class ButtonListenerFirstName implements ActionListener
    {
        public void actionPerformed (ActionEvent e )
        {
             fName = employeeDetails1.getText();
            System.out.println("and This is the employes first name :"+ fName);         
        } 
    }

    public class ButtonListenerLastName implements ActionListener
    {
        public void actionPerformed (ActionEvent e )
        {
             lName = employeeDetails2.getText();
            System.out.println("and This is the employes Last name :"+ lName);      
        } 
    }




    public static Department getDepartment()
    {
        return null;
    }


}


Thanks 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:54:04+00:00Added an answer on May 26, 2026 at 5:54 am

    The only variable which an be null on that line and cause that exception is the employeeDetails2, so you must look back in your code to see if and where you assign an object to that variable. On doing this, you’ll see that you assign an object to a employeeDetails2 in the guiEmployee1 class’s constructor, but immediately above this you re-declare the variable in the constructor. Thus it is the local employeeDetails2 variable which has been assigned an object, not the class field which is never initialized. This is called variable shadowing. The solution is not to redeclare the variable in the constructor.

    i.e.:

    public guiEmployee1()
    {
        JButton submit;
        JButton b1;
    
        System.out.println("cabanas");
    
        JFrame frame = new JFrame();
        employeeDetails1 = new JTextField(10);
    
        //  JTextField employeeDetails2; ***** comment out this line *****
        employeeDetails2 = new JTextField(10);
    

    Next, you’ll want to re-name your variables so that your code becomes “self-commenting”. In other words, rather than giving the variable such a general name as employeeDetailsX, why not instead call it lastNameField, and the one before it firstNameField, and the “b1” button to submitButton? That way when debugging your code, you’ll know exactly what it’s supposed to be doing?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really new at Java. I have been trying to allow text selection
I am new to Java and am trying to run a program using Eclipse.
Just a warning: I'm completely new to Java and am trying to teach myself
I'm new to java and I'm trying to swap out the text on a
Hey I'm new to java servlets and I am trying to write one that
I'm pretty new to Java ME and i'm trying to use Microlog to handle
I know something about Java but completely new to Enterprise Java. I'm trying my
I'm trying to deploy a java application to appspot (google appengine). I'm new to
I am very new to processing.org and Java. I am trying to store objects
I'm trying to allow the user to change the title of a window in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.