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