0

I keep getting this error when I run this code and I do not know how to fix it. I've been doing Java for about 3 months now and could use some help. Basically the program asks the user to enter in the pay rate and the hours for each employee. Pay rate, hours and employee number are all arrays that are going to share the same index with each other. When the user enters in the pay rate and hours for the employee, the program returns the gross pay for that employee. Except this error keeps popping up. Please help!

Exception in thread "main" java.lang.NullPointerException
    at chapter.pkg7.lab.Payroll.setPayRate(Payroll.java:41)
    at chapter.pkg7.lab.Chapter7Lab.main(Chapter7Lab.java:45)
Java Result: 1

Here's the code:

package chapter.pkg7.lab;

import java.util.Scanner;

public class Chapter7Lab {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
 Payroll newEmployee = new Payroll();
       int[] employeeNum = newEmployee.getEmployeeID();
       double pay;
       int hour;

       for (int i = 0; i < employeeNum.length; i++)
       {
           System.out.println("Please enter the Pay Rate for Employee #" + employeeNum[i]);
           pay = keyboard.nextDouble();
           newEmployee.setPayRate(pay, i);

           System.out.println("Please enter the hours for Employee #" + employeeNum[i]);
           hour = keyboard.nextInt();
           newEmployee.setHours(hour, i);

           System.out.println("Gross pay is: " + newEmployee.getWages(i));
       }



package chapter.pkg7.lab;

public class Payroll {
    private int[] employeeID = new int[] {5658845, 4520125, 7895122, 
        8777541, 8451277, 1302850, 7580489};

    private int[] hours;
    private double[] payRate;
    private double wages;


   public double getWages(int index) {
      wages = hours[index] * payRate[index];
      return wages;
    }


   //Accessors and Mutators
    public int[] getEmployeeID() {
        return employeeID;
    }

    public void setEmployeeID(int[] employeeID) {
        this.employeeID = employeeID;
    }

    public int[] getHours() {
        return hours;
    }

    public void setHours(int hours, int index) {
        this.hours[index] = hours;
    }

    public double[] getPayRate() {
        return payRate;
    }

    public void setPayRate(double payRate, int index) {
        this.payRate[index] = payRate;
    }



}
2
  • Post the error/stacktrace Commented Nov 14, 2013 at 22:09
  • @Michael that's impressive how you missed that information at the very beginning of the post :) Commented Nov 14, 2013 at 22:11

3 Answers 3

2

You've declared payRate (and hours and wages) as arrays, but it's an uninitialized instance variable, so it's null.

Initialize it:

private double[] payRate = new double[employeeID.length];

(and likewise for hours and wages).

Sign up to request clarification or add additional context in comments.

Comments

0

Add a constructor to your Payroll class that initialises the two arrays hours and payRate like this:

public Payroll(){
   hours = new int[employeeID.length];
   payRate = new double[employeeID.length];
}

Then when you create the Payroll object in your main class as you're currently doing with the line: Payroll newEmployee = new Payroll(); the arrays will be initialised.

1 Comment

Thank you! I'm pretty new to Java. Still learning the concepts and basics but I see where I went wrong
0
NullPointerException 

Arise when you won't initialize variable or object and try to access that varibale or object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.