title: How to solve java.lang.NullPointerException while reading same file in two java programs?
body: <p>I am trying to run one Java program from another. One makes a copy of a file and another reads that copied file. But while doing so, I get <code>java.lang.NullPointerException</code>. Can someone explain why? And how can I solve this?</p>
  
  <p>Following is my code :</p>
  
  <p>FirstProgram.java</p>
  
  <pre><code> package test;
  
   import java.io.BufferedReader;
   import java.io.BufferedWriter;
   import java.io.File;
   import java.io.FileNotFoundException;
   import java.io.FileReader;
   import java.io.FileWriter;
   import java.io.IOException;
  
   import secondprogram3.SecondProgram;
  
   public class FirstProgram {
  
    public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException {
  
      BufferedReader input = null;
      BufferedWriter output = null;
  
      try {
          String filePath = "1.txt";
          input = new BufferedReader(new FileReader(filePath));
          output = new BufferedWriter(new FileWriter("input.txt"));
          String line="";
          while ((line = input.readLine()) != null) {
              output.write(line);
              output.newLine();
          }
  
  
      } finally {
          output.close();
          input.close();
      }
  
  
      String args[] = {"Hello"};
      SecondProgram.main(args);
    }  
  }
  </code></pre>
  
  <p>SecondProgram.java</p>
  
  <pre><code>package secondprogram3;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  
  
  public class SecondProgram {
  
      public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException {
  
          BufferedReader input = null;
  
          try {              
            input = new BufferedReader(new FileReader("input.txt")); // I get java.lang.NullPointerException here
  
            String line="";
            while ((line = input.readLine()) != null) {
               System.out.println(line);
            }
          }  finally {
            input.close();
          }
      }
  }
  </code></pre>