0

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

Rijak Singh
  • 67
  • 1
  • 9
  • 2
    Does this answer your question? [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) – OH GOD SPIDERS Feb 23 '21 at 13:39
  • 2
    You are using the `OUTPUT_PATH` environment variable for your `FileWriter`. Are you sure that codechef has this environment variable set? – Ivar Feb 23 '21 at 13:41
  • 2
    `System.getenv("OUTPUT_PATH")` is returning `null`, because `OUTPUT_PATH` is not set in the environment on whatever system you are running this on. This is then causing FileWriter to go: Oi, I can't create a writer to a non-existent file, and throws an exception as a result. – rzwitserloot Feb 23 '21 at 13:44
  • oh ok thank you so much, instead of buffered writer I used system.out.print and it works! – Rijak Singh Feb 23 '21 at 14:33

0 Answers0