I wanted to read data from multiple files simultaneously but I am getting NULL error.
Could anyone help in pointing out my error in the code.
I am not sure if I can make arrays of FileInputStream and DataInputStream the way I have done.

import java.io.*;
class Count1
{
	static FileInputStream fstream[];
	static DataInputStream in[];
    static BufferedReader br[] ;
public static void main(String args[])
  {
      int count=0;
	   try{    
    
   // FileInputStream fstream[] = null ;
    int i=0;
    	fstream[i++]=new FileInputStream("results1.txt");  // Null error in this line
    	fstream[i++]=new FileInputStream("results2.txt");
    	fstream[i++]=new FileInputStream("results3.txt");
    	fstream[i++]=new FileInputStream("results4.txt");
    

    //DataInputStream in[] = null;
    //BufferedReader br[] = null;
    for(int j=0; j<i; j++)
    	{
    	in[j]=new DataInputStream(fstream[j]);
    	
        br[j] = new BufferedReader(new InputStreamReader(in[j]));
    	}    
   
    /*
	
	Working on files 
	
	*/
        
    for(int k=0; k<i; k++)
    in[k].close();
    }catch (Exception e){
      System.out.println(e.getMessage());
    }
  }
}

Recommended Answers

All 3 Replies

FileInputStream fstream[] is not initialized and therefore is null. Before you can write the code in lines 14 and beyond, you have to initialize the array itself:

fstream = new FileInputStream[array size]

If the array size is dynamic, you might want to consider a list instead.

Done!! Thanku so much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.