I was solving an algorithm problem using java and got this runtime error:
Exception in thread "main" java.lang.NullPointerException
at java.io.FileOutputStream.<init>(FileOutputStream.java:203)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at java.io.FileWriter.<init>(FileWriter.java:63)
at Main.main(Main.java:50)
I don't have much experience with java so I am sharing my code here:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Main {
// Complete the breakingRecords function below.
static int maxXv(int[] gymXp, int strength) {
int temp = 0;
int xp = 0;
int max = gymXp[0];
for (int i = 0; i < gymXp.length; i++){
if (gymXp[i] > max) {
max = gymXp[i];
}
}
int digiSum = 0;
int num = 0;
for (int j = 0; j < gymXp.length; j++){
if (j == gymXp.length-1){
xp += strength*gymXp[j];
System.out.println("Battling Cuz im last, strength "+strength+ " xp="+ xp);
continue;
}
if (gymXp[j]==max){
xp += strength*gymXp[j];
System.out.println("Battling Cuz im biggest, strength "+strength+ " xp="+ xp);
continue;
}
num = strength;
digiSum = 0;
while(num!=0){
digiSum = digiSum + num % 10;
num = num/10;
}
strength += digiSum*digiSum*digiSum;
System.out.println("trained and now , strength "+strength+ " xp="+ xp);
}
return xp;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int strength = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] gymXp = new int[n];
String[] xpItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int xpItem = Integer.parseInt(xpItems[i]);
gymXp[i] = xpItem;
}
int result = maxXv(gymXp, strength);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
I don't understand what the error is about and when I run it on hackerrank it works just fine but it's giving me the above error on codechef(which is where I was solving this problem).
Please help me as I have been stuck for hours with this issue