speedracer
speedracer

Reputation: 93

Calculating with Java Arrays

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

Upvotes: 1

Views: 2976

Answers (3)

Jackson Ha
Jackson Ha

Reputation: 682

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];
}

Upvotes: 0

JRaymond
JRaymond

Reputation: 11782

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!

Upvotes: 1

Reimeus
Reimeus

Reputation: 159874

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];

Upvotes: 1

Related Questions