1

first time poster here. I am at the end of my rope, ready to give up my pursuit of a career in programming. I failed to turn in my last assignment because I couldn't get my program to work. Now, I'm doing some extra practice on my own. A simple practice assignment using arrays and I can't figure out the last bit of it. Can someone please tell me what I'm doing wrong here? When I run it, I can enter the data, but the program quits prior to displaying the output with this error:

Exception in thread "main" java.lang.NullPointerException
     at Payroll.getGrossPay(Payroll.java:47)
     at PayrollCalc.main(PayrollCalc.java:32)

I can display hours and payRate separately, but I cannot display grossPay. I'm also not happy with my constructor, but not quite sure what to do with it. The problem required me to initialize the array in the Payroll class.


public class Payroll {
    private final int NUM_EMPLOYEES = 3;
    private int[] employeeId = {5658845, 4520125, 7895122}, empId, hours;
    private double[] payRate, wages, grossPay;

    public Payroll()
{
    empId = employeeId;
}
public void setEmployeeId(int[] employeeId)
{
    empId = employeeId;
}
public void setEmpHours(int[] empHoursIn)
{
    hours = empHoursIn;
}
public void setEmpPayRate(double[] empPayRateIn)
{
    payRate = empPayRateIn;
}
public int[] getEmployeeId()
{
    return empId;
}
public int[] getEmpHours()
{
    return hours;
}
public double[] getEmpPayRate()
{
    return payRate;
}
public double[] getGrossPay()
{
    for (int count = 0; count < NUM_EMPLOYEES; count++)
    {
        grossPay[count] = getEmpPayRate()[count] * getEmpHours()[count];
    }

    return grossPay;
}
}

import java.util.Scanner;

public class PayrollCalc 
{
public static void main(String[] args) 
{
    int count;
    final int NUM_EMPLOYEES = 3;
    int[] empHours = new int[NUM_EMPLOYEES];
    double[] empPayRate = new double[NUM_EMPLOYEES];
    Scanner keyboard = new Scanner(System.in);
    Payroll payroll = new Payroll();

    for (count = 0; count < NUM_EMPLOYEES; count++)
    {
        System.out.print("Enter total hours for employee " +  
                           payroll.getEmployeeId()[count] + ": ");
        empHours[count] = keyboard.nextInt();
        payroll.setEmpHours(empHours);

        System.out.print("Enter pay rate for employee " + 
                           payroll.getEmployeeId()[count] + ": ");
        empPayRate[count] = keyboard.nextDouble();
        payroll.setEmpPayRate(empPayRate);
    }

    System.out.println("\nEMPLOYEE ID\tGROSS PAY");
    System.out.println("-----------     ---------");

    for (count = 0; count < NUM_EMPLOYEES; count++)
    {
        System.out.println(payroll.getEmployeeId()[count] + "\t\t" + 
                           payroll.getGrossPay()[count]);           
    }
  }
  }

Thanks in advance for your help!

Stef

2
  • 5
    Something that shouldn't be null is null at line 47. Which line is that? The "java:XX" part is referring to a line number in your code. Check your initialization of the arrays. Oh and, don't give up! Commented Oct 30, 2012 at 19:31
  • @Keyser Thanks for the encouragement. I've been on the verge of tears for several days now. Not over this program, but over the program I just could not figure out. Not being able to figure out something this simple was even more discouraging, though. Commented Oct 31, 2012 at 1:24

3 Answers 3

1

Doesn't look like you ever initialized the grossPay array - see if this makes a difference:

private double[] payRate, wages;
private double[] grossPay = new double[NUM_EMPLOYEES];

As a tip, when starting out it's probably best to initialize all your variables as soon as you can, which in your case is mostly at construction time. When you get a little more comfortable then you initialize when you need things - for example:

public double[] getGrossPay()
{
  if (grossPay == null) {
    grossPay = new double[NUM_EMPLOYEES]; 

    for (int count = 0; count < NUM_EMPLOYEES; count++)
    {
      grossPay[count] = getEmpPayRate()[count] * getEmpHours()[count];
    }
  }
  return grossPay;
}

Good luck!

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

Comments

1

Your NullPointerException is occuring here:

grossPay[count] = getEmpPayRate()[count] * getEmpHours()[count];

as you haven't initialised the double array grossPay (or payRate or wages). You could use:

private double[] grossPay = new double[NUM_EMPLOYEES];

also you'll need:

private double[] payRate = new double[NUM_EMPLOYEES];
private double[] wages = new double[NUM_EMPLOYEES];

Comments

0

array grossPay was never initialized inside Payroll in public double[] getGrossPay().

you need to initialize the size of the array before using it.

probably best would be to add in constructor:

public Payroll() {
grossPay= new double[NUM_EMPLOYEES];
}

1 Comment

Everyone, THANK YOU! How could I overlook that?! I know better, but I think I've been so frustrated by all of this that I can't think straight. My program works perfectly now!

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.