Java: Null Pointer Exception

+1 Homer Simpson · December 16, 2014
I've been trying to figure this out for about an hour now and I can't seem to figure it out. So I have a class which is called Weekly_Payroll. I also have a driver class. In my driver class I have to allow the user to create multiple instances of the Weekly_Payroll class. So, I created a global instance variable Weekly_Payroll employees[]; In the main method i have a menu. The first option they can choose is HireNewEmployees. That calls the function hireNewEmployees(). The first question is how many employees would u like to hire. they enter in a number and then i go ahead and set employees = new Weekly_Payroll[theNumber].
The problem is none of my other methods work. They revolve around this array so I could only assume thats my issue. I get a nullpointerexception. I've looked everywhere and couldn't find out why. So I though maybe the rest of my program cant see the array so I tested to check if the array was null. Thats not the case, So I'm like well maybe the rest of the program cant see how large it is.. So I tested for that and it can! So, I'm lost. D: The whole program itself is hideous so I'll try and paste the parts I think might be causing the issue. D: Any help would be appreciated.
public class Driver{
private static Scanner input = new Scanner(;

private static Weekly_Payroll employees[]; // Heres the array!!!

public static void main(String args[]) {
System.out.println("Welcome to the Fictional Company Hiring/Payroll System.");

int action = 0;

System.out.println("*1. Hire new Employees          2. Update Employee Info                  *");
System.out.print("What would you like to do: ");
action = input.nextInt();

case 1:
case 2:
}while(action != 0);


If you choose any option before hiring employees you get a message telling you need to hire employees first. 
// Fill in the Weekly_Payroll array
private static void hireEmployees(){
if(employees != null){
System.out.println("There is not enough money in our budget to hire new employees at this time");
} else {
int total;
System.out.println("How many employees will you be hiring?");
total = input.nextInt();
employees = new Weekly_Payroll[total]; // Setting the size of the array!!

for(int i = 0; i < employees.length; i++){
 getEmployeeInfo(employees, i);

Heres the getEmployeeInfo method

private static void getEmployeeInfo(Weekly_Payroll employee, int element){
String name, jobTitle;
int ID = element + 1;

System.out.println("What is the new employees name?");
name = input.nextLine();
System.out.println("Current job openings: Supervisor, Associate, & Clerk");
System.out.println("Which position will " + name + " be filling?");
jobTitle = input.nextLine();
System.out.println("" + name + " has been issued the ID #" + ID);
System.out.println("****Employee Has Been Processed****");

employee = new Weekly_Payroll(name, jobTitle, ID);

All of this runs. The issue comes in when I try to call any of the other functions. They all give me a nullpointerexception. 

private static void updateEmployeeInfo(){
if(employees != null){
String name;
String jobTitle;
int ID, element;
System.out.print("Enter the ID # of the employees who's information you wish to update: ");
ID = input.nextInt();
element = ID - 1;

System.out.println("What is the employees name?");
name = input.nextLine();

System.out.println("Current job openings: Supervisor, Associate, & Clerk");
System.out.println("Which position will " + name + " be filling?");
jobTitle = input.nextLine();
System.out.println("****Employee Updates Have Been Processed****");
} else {
System.out.println("Our records don't seem to have any employees on file.");


As soon as I enter the employees name is when it happens. 
If you want to see the full code. 

Post a Reply


Oldest  Newest  Rating
0 Homer Simpson · December 16, 2014
Thanks for taking the time out to answer my question! :D You're the best. I changed employees from and array to an ArrayList and so far it seems to work. I have to go through and change somethings to know for sure but its looking good. ^_^ I get what you're saying about them being static but thats how the teacher taught the class to do it so I'm just going with it! Thanks again!! 
0 Troy Jones · December 16, 2014

This is technical shit and you probably shouldn't freak out if you don't know it.

0 Kuroodo Ditory · December 17, 2014
In non-technical terms:

You have an array of tuna fish without a name, jobTitle, and ID.

You clone one of the tuna fish and give it to a method. Then that method says that the tuna fish is a tuna fish but with a name, jobTitle, and ID; even though it already was a tuna fish.

The proper way of doing is to just give/set the cloned tuna fish's name, jobTitle, and ID in the method instead of saying that the tuna is a tuna fish (as explained above (getters and setters are your best friend)).

Since the methods are static and the tuna fish array is static (atleast I think it's the reason why), whatever you do to the cloned tuna will be also be added to the original tuna that you got a clone from.

But yeah ArrayList's are awesome. Instead of having a bunch of tuna lying around, you'll have a basket full of tuna!
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator