0

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 java.lang.NullPointerException. Can someone explain why? And how can I solve this?

Following is my code :

FirstProgram.java

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

SecondProgram.java

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();
        }
    }
}
parul71625
  • 193
  • 2
  • 4
  • 14

0 Answers0