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 6656587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:39:43+00:00 2026-05-26T01:39:43+00:00

I am trying to create a program for an online Java course. This program

  • 0

I am trying to create a program for an online Java course. This program includes an Employee class and a Name class. I have to create multiple Employee objects and prompt the user to enter the employee’s name. I am storing all the Employee objects in an employee array.

Here’s the code:

    //Creates employee array with the number of array elements being however many          
    //employees there are:
    Employee employee[] = new Employee [ numEmp ];

    for( int j = 0; j < numEmp; j++ )
    {
        System.out.println( "Please enter the first name of employee number "
                + ( j + 1 ) );
        Scanner input2 = new Scanner( System.in );
        String nameF = input2.nextLine();

        //This should set the employee object at employee array element "j" to the          
        //String nameF
        employee[ j ].setFirstName( nameF );

The problem is that the compiler, while running the program, says that the last line is a NullPointerException.
I’m not sure what I am doing wrong. Any suggestions?

Thanks!
-Sean

  • 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-26T01:39:44+00:00Added an answer on May 26, 2026 at 1:39 am

    You created a new array with a size of numEmp, but the default value for each element is null. This means that the array initially contains numEmp null references. You need to use new to instantiate each Employee object before you can call methods on them.

    You can either do this immediately after you create the array:

    Employee employee[] = new Employee [ numEmp ];
    for( int j = 0; j < numEmp; j++ )
    {
        employee[j] = new Employee();
    }
    

    Or you could do it inside your existing loop, just before you first need to use the object:

    employee[j] = new Employee();
    employee[j].setFirstName(nameF);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a Ruby program that will be running online
I have a problem with my WPF program. I'm trying to create an object
I'm trying to create a desktop program that will interact with the online todo
I have been trying to create a timer program with VB 2010 to the
I am trying to create a program over multiple files that reads out the
I am trying to create a program that reads multiple choice answers from a
Based on this question which was closed rather quickly: Trying to create a program
I have a web service I'm trying to load test. I created a program
I am trying to create a program which will show me the status of
I am currently trying to program an online drawing program using the HTML5 canvas.

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.