-1

I seem to get a NullPointerException in my code at line 15, but I just cannot understand why. I have been stuck on this for some hour now, and I don't know how to fix it. I have read up on what NullPointerException is, and I think I have a clear grasp of what it is, but I thought my if-statement would fix it, but apparently not.

Here is the code:

package learning;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Index{
    public static void main(String args[]){
        Index indexObject = new Index();
        String filePath = "C:\\Users\\Edvin\\Desktop\\inputPrices.txt";
        Scanner inputScanner = new Scanner(System.in);
        int average = 0;
        if(indexObject != null){
            average = indexObject.findAverage(getFileInfo(filePath).split(","));
        }
        else{
            System.out.println("Object instance is null. Terminating program.");
        }

        int currentItemInput = inputScanner.nextInt();
        if(currentItemInput<average && currentItemInput != 0){

        }
    }

    private static String getFileInfo(String x){
        File listOfPrices = new File(x);
        String error = "An error occurred";
        try{
            BufferedReader getInfo = new BufferedReader(new FileReader(listOfPrices));

            String prices = getInfo.readLine();

            while(prices != null){
                prices = getInfo.readLine();
            }
            return prices;
        }
        catch(FileNotFoundException e){
            System.out.println("Couldn't find File");
            System.exit(0);
            return error;
        }
        catch(IOException e){
            System.out.println("An I/O error occurred");
            System.exit(0);
            return error;
        }
    }

    public int findAverage(String[] tempIndivPrices){
        int x = 0;
        int y = 0;
        int total;
        int [] indivIntPrices = null;
        for(String i : tempIndivPrices){
            indivIntPrices[x] = Integer.parseInt(tempIndivPrices[x]);
            x++;
        }
        for (int element : indivIntPrices){
            total =+ indivIntPrices[element];
            if(element==indivIntPrices.length-1){
                int result = total / indivIntPrices.length;
                System.out.println(result);
                return result;
            }
        }
        return 0;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arcthor
  • 53
  • 4
  • 8

3 Answers3

2

You are declaring an array but you initialize it to null in this line: int [] indivIntPrices = null;

indivIntPrices is now a null pointer. You are then trying to access this array two lines later indivIntPrices[x] = ... which is not going to work because the variable is not pointing to an array.

To solve this, allocate a new array of integers with the same size as the input array:

int [] indivIntPrices = new int[tempIndivPrices.length];
Boann
  • 48,794
  • 16
  • 117
  • 146
Philipp
  • 67,764
  • 9
  • 118
  • 153
1

Try to check this part of the code: getFileInfo(filePath)

Are you getting the properly result of it? It's the only part that you can get this error.

vinibarr
  • 500
  • 3
  • 5
0

Remove this loop:

while(prices != null){
    prices = getInfo.readLine();
}

Then, it works.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
harish
  • 1