title: Java Runtime Error: Exception in thread "main" java.lang.NullPointerException
body: <p>I was solving an algorithm problem using java and got this runtime error:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.NullPointerException
      at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:203)
      at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:101)
      at java.io.FileWriter.&lt;init&gt;(FileWriter.java:63)
      at Main.main(Main.java:50)
  </code></pre>
  <p>I don't have much experience with java so I am sharing my code here:</p>
  <pre><code>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 &lt; gymXp.length; i++){ 
               if (gymXp[i] &gt; max) {
                   max = gymXp[i];
               }
          }
          int digiSum = 0;
          int num = 0;
          for (int j = 0; j &lt; gymXp.length; j++){
              if (j == gymXp.length-1){
                  xp += strength*gymXp[j];
                  System.out.println(&quot;Battling Cuz im last, strength &quot;+strength+ &quot; xp=&quot;+ xp);
                  continue;
              }
              if (gymXp[j]==max){
                  xp += strength*gymXp[j];
                  System.out.println(&quot;Battling Cuz im biggest, strength &quot;+strength+ &quot; xp=&quot;+ xp);
                  continue;
              }
              num = strength;
              digiSum = 0;
              while(num!=0){
                  digiSum = digiSum + num % 10;
                  num = num/10;
              }
              strength += digiSum*digiSum*digiSum;
              System.out.println(&quot;trained and now , strength &quot;+strength+ &quot; xp=&quot;+ 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(&quot;OUTPUT_PATH&quot;)));
  
          int n = scanner.nextInt();
          scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
          
          int strength = scanner.nextInt();
          scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
  
          int[] gymXp = new int[n];
  
          String[] xpItems = scanner.nextLine().split(&quot; &quot;);
          scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
  
          for (int i = 0; i &lt; 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();
      }
  }
  
  </code></pre>
  <p>I don't understand what the error is about and when I run it on <a href="https://www.hackerrank.com" rel="nofollow noreferrer">hackerrank</a> it works just fine but it's giving me the above error on <a href="https://www.codechef.com" rel="nofollow noreferrer">codechef</a>(which is where I was solving this problem).</p>
  <p>Please help me as I have been stuck for hours with this issue</p>