0

I get error that variable fin might not have been initialized in the following program. Please clear me the concept about initialization. :

import java.io.*;
class ShowFile
{
    public static void main(String args[])
    throws IOException
    {
        int i;
        FileInputStream fin;
        try
        {
            fin=new FileInputStream(args[0]);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File not found");
        }
        catch(ArrayIndexOutOfBoundsException e)
        {System.out.println("Array index are out of bound");}
        do
        {
            i=fin.read();
            System.out.println((char) i);
        } while(i!=-1);
        fin.close();
    }
}

but in the following code, I don't get such error

import java.io.*; 

class ShowFile { 
  public static void main(String args[]) 
  { 
    int i; 
    FileInputStream fin; 

    // First, confirm that a file name has been specified. 
    if(args.length != 1) { 
      System.out.println("Usage: ShowFile filename"); 
      return; 
    } 

    // Attempt to open the file. 
    try { 
      fin = new FileInputStream(args[0]); 
    } catch(FileNotFoundException e) { 
      System.out.println("Cannot Open File"); 
      return; 
    } 

    // At this point, the file is open and can be read. 
    // The following reads characters until EOF is encountered. 
    try { 
      do { 
        i = fin.read(); 
        if(i != -1) System.out.print((char) i); 
      } while(i != -1); 
    } catch(IOException e) { 
      System.out.println("Error Reading File"); 
   } 

    // Close the file. 
    try { 
      fin.close(); 
    } catch(IOException e) { 
        System.out.println("Error Closing File"); 
    } 
  } 
}

Why so ? Please help me. and sorry for the inconvenience in reading. It's my first post so I don't know that exactly how to post.

Thank you.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Read up on variable initialization here: http://stackoverflow.com/questions/1560685/why-must-local-variables-including-primitives-always-be-initialized-in-java – Drejc Nov 22 '14 at 16:36
  • The difference between both: You exit the method when a checked exception occurs in the second case (`return;`) which guaranties that there is no read access to `fin`, in the first case you simply procede with the do-while loop (-> a read access will happen). – fabian Nov 22 '14 at 16:46
  • Thank you fabian. But after adding return in first case, still am getting the same error : " ShowFile1.java:21: error: variable fin might not have been initialized i=fin.read(); ^ 1 error" – Abhilash Mandaliya Nov 22 '14 at 17:11

3 Answers3

0

You have this try-catch block

try {
    fin=new FileInputStream(args[0]);
} catch(...) {
 .....
}

where you catch a potential FileNotFoundException then outside of it you you access fin

If an exception occurs in the first block, then fin will be uninitialized and you will end up with an NPE

Move the reading inside the first block.

try {
    fin=new FileInputStream(args[0]);
    do {
        i=fin.read();
        System.out.println((char) i);
    } while(i!=-1);
} catch(...) {
 .....
} finally {
    if(fin != null) {
        try { fin.close(); } catch(IOException e) {}
    }
}
A4L
  • 17,353
  • 6
  • 49
  • 70
0

A variable needs to be initialized before it is used. In your case, for example, this line fin=new FileInputStream(args[0]); might throw an exception (suppose, the file does not exist), you catch the exception, print it out, and then do this i=fin.read(); Here fin might not have been initialized.

In general, it is a good idea to always initialized all variables when you declare them:

int i = 0;
InputStream fin = null;

Next time, please also post the actual error message you need help with. It helps not having to try to "compile" your code mentally to guess what you are talking about.

Dima
  • 39,570
  • 6
  • 44
  • 70
0

For variables declared and not initializsed inside a method, there must not be any possible execution path from the declaration to any point of the variable's use. This is checked by the compiler, and it won't let you compule.

Fields in a class are always initiialzed, possibly with zero, so it does not apply to these.

laune
  • 31,114
  • 3
  • 29
  • 42