0

I'm getting a nullpointerexception at weeklywagess.Paycheck.calculatePayArray(Paycheck.java:229).

  1. The project assignment is to take the information from the file njgov56 and enter it in an array.

  2. I do this by entering the information into 4 different arrays.

  3. 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.

  4. But for some reason it takes the information from arrays but does not keep the values.

  5. 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
            }
        }         
    }
 
}
  • How is JavaScript involved? Can you reduce the code to what's actually relevant? – Dave Newton Feb 25 '18 at 23:56
  • javascript !== java - please check your tags – Jaromanda X Feb 25 '18 at 23:56
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Matt Feb 25 '18 at 23:59
  • I edited it, i figured having everything would help, and when i posted this it recommended taging this with javascript. – Dr_Gregory_House Feb 26 '18 at 00:00
  • I'm flagging this as a duplicate. When you get a NullPointerException your Java compiler will give you a stack trace. Use this to determine the line that threw the exception and you will see which object is `null`. Then you must find out why - did you fail to initialize it? Running your code in a debugger and stepping through it will be instructive. – Matt Feb 26 '18 at 00:01
  • I initialized the array with the information I got from the main. The line i get the exception is if (holidays[index].equalsIgnoreCase("Yes")). Even though here I clearly take the array from the main and set it to the array holiday here 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]); } } – Dr_Gregory_House Feb 26 '18 at 00:05
  • Right, so it seems `holiday[index]` is not set. This gives you a clue. If you're not sure how to use the debugger put it on your TODO list, but for now, you can use print statements to show you some information at different parts of your program. Find all the places where you touch the `holidays` array and make sure it looks like what you think it should. – Matt Feb 26 '18 at 00:08
  • In my recieveHolidayArray I have print statements and it displays what I want to be inside the array by displaying holidays[count] data. which is 18 yes or nos this makes me believe the information is set because how would the information not be set if the array holidays[] displays the information in the method. But under the same class but in a different method that same array of information is empty. Clearly its a NullPointerException problem but logically i don't see why it would be. – Dr_Gregory_House Feb 26 '18 at 00:23
  • i figured it out. I was overthinking the entire thing, all I had to do was make holiday[] array equal the array I sent to the class. thank you for the help :D – Dr_Gregory_House Feb 26 '18 at 00:55
  • Instead of using 'holidays[index].equalsIgnoreCase("Yes")' , you should always use '"Yes".equalsIgnoreCase(holidays[index])'. In this way, you will never got a NullPointerException. – Vicky Feb 26 '18 at 21:57

0 Answers0