I'm getting a nullpointerexception at weeklywagess.Paycheck.calculatePayArray(Paycheck.java:229).
The project assignment is to take the information from the file njgov56 and enter it in an array.
I do this by entering the information into 4 different arrays.
Then I have to take the information from the array and send it to the class Paycheck so I can determine the wages of the employees.
But for some reason it takes the information from arrays but does not keep the values.
- Once the program starts to calculate the wages it says the arrays in the class are null.
I can't figure out why this is happening.
I hope it is something simple I am missing.
package weeklywagess;
import java.util.*;
import java.io.*;
public class WeeklyWages
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//Open the file for reading
int ctr=0;
try
{
Scanner inputStream = new Scanner(new File("C:\\Users\\House\\Desktop\\njgov56.txt"));
while (inputStream.hasNextLine())
{
ctr=ctr+1;
inputStream.nextLine();
}
ctr=ctr/4;
Paycheck array = new Paycheck();
array.getArraySize(ctr);
String[] names = new String[ctr];
double[] hours = new double[ctr];
double[] rate = new double[ctr];
String[] holiday = new String[ctr];
Scanner inputStream1 = new Scanner (new File ("C:\\Users\\House\\Desktop\\njgov56.txt"));
for (int count=0; count <ctr;count++)
{
names[count]=inputStream1.nextLine();
hours[count]=inputStream1.nextDouble();
rate[count]=inputStream1.nextDouble();
holiday[count]=inputStream1.next();
if (count==ctr)
System.out.println();
else
inputStream1.nextLine();
}
array.recieveHoursArray(hours);
array.recieveRateArray(rate);
array.recieveHolidayArray(holiday);
array.recieveNameArray(names);
array.calculatePayArray();
}
catch(FileNotFoundException e)
{
System.out.println("Error opening file! Exiting");
System.exit(0); //quit the program
}
package weeklywagess;
import java.util.Scanner;
public class Paycheck
{
static Scanner console = new Scanner(System.in);
private double wages;
private double rate;
private double hours;
private int arraySize;
private double wage[];
private double hour[];
private double rates[];
private String names[];
private String holidays[];
private String holiday;
private String name;
/////////////Constructor///////////////////////////////////////////////////////////////////////
Paycheck()
{
wages=0;
rate=0;
hours=0;
holiday = "";
//System.out.print("I'M INSIDE OF PAYCHECK!");
}
}
////////////////////////////////////////////////////////
public void getArraySize(int x)
{
arraySize=x;
}
////////////////////////////////////////////////////////
public void recieveHoursArray(double x[])
{
for (int count=0;count < arraySize;count++)
{
hour = new double [arraySize];
hour[count]=x[count];
System.out.println("hour: "+hour[count]);
}
}
////////////////////////////////////////////////////////
public void recieveRateArray(double x[])
{
for (int count=0;count < arraySize;count++)
{
rates = new double [arraySize];
rates[count]=x[count];
System.out.println("rate: "+rates[count]);
}
}
////////////////////////////////////////////////////////
public void recieveHolidayArray(String x[])
{
for (int count=0;count < arraySize;count++)
{
holidays = new String [arraySize];
holidays[count]=x[count];
System.out.println("holiday: "+holidays[count]);
}
}
////////////////////////////////////////////////////////
public void recieveNameArray(String x[])
{
for (int count=0;count < arraySize;count++)
{
names = new String [arraySize];
names[count]=x[count];
System.out.println("names: "+names[count]);
}
}
////////////////////////////////////////////////////////
public void calculatePayArray()
{
for (int index=0;index<arraySize;index++)
{
if (holidays[index].equalsIgnoreCase("Yes"))
{
wage[index] = rates[index] * 2 * hour[index];
}
else
{// Non-holiday pay
if (hour[index] > 40.0) // Line 8
{
wage[index] = 40.0 * rates[index] + 1.5 * rates[index] * (hour[index] - 40.0); // Line 9
}
else // Line 10
{
wage[index] = hour[index] * rates[index]; // Line 1
}
}
}
}