Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cannot find symbol" when trying to close I/O objects

public static int howMany(String FileName)
{
    BufferedReader br = null;

    try
    {
        FileInputStream fis = new FileInputStream(FileName);
        DataInputStream dis = new DataInputStream(fis);
        br = new BufferedReader(new InputStreamReader(dis));
    }
    catch (FileNotFoundException e)
    {
        System.out.print("FILE DOESN'T EXIST");
    }
    finally
    {
        fis.close();
        dis.close();
        br.close();
    }


    String input;
    int count = 0;

    try
    {

        while ((input = br.readLine()) != null)
        {
            count++;
        }

    }
    catch (IOException e) 
    {
        System.out.print("I/O STREAM EXCEPTION");
    }


    return count;
}

For some reason, I cannot close any I/O objects. fis.close(), dis.close(), br.close() all give me cannot find symbol even though I imported all the I/O library (import java.io.*;) and initiated all the objects.

like image 965
Matthew Loch Avatar asked Mar 07 '13 21:03

Matthew Loch


People also ask

How do I fix error Cannot find symbol?

In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

What does error Cannot find symbol mean?

The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.

Why is Java saying Cannot find symbol?

Misspelled method name Misspelling an existing method, or any valid identifier, causes a cannot find symbol error. Java identifiers are case-sensitive, so any variation of an existing variable, method, class, interface, or package name will result in this error, as demonstrated in Fig.

What does Cannot resolve method mean in Java?

“cannot resolve symbol” means that the Java compiler cannot locate a symbol referenced in your Java source code. The causes for this common error include: A missing class file. A faulty CLASSPATH. A symbol name is mispelled or miscapitalized.


2 Answers

 BufferedReader br = null;
 FileInputStream fis =null;
 DataInputStream dis null;
 try {
     fis = new FileInputStream(FileName);
     dis = new DataInputStream(fis);
     br = new BufferedReader(new InputStreamReader(dis));
 }

Put them out of your try block, so that your finally block can see the variables.

like image 65
PermGenError Avatar answered Oct 02 '22 22:10

PermGenError


You have to declare all the streams outside the try block, otherwise they won't be visible in the finally block:

FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;

Alternatively, you could use Java 7's new try-with-resources syntax to automate resource closing.

like image 31
Óscar López Avatar answered Oct 03 '22 00:10

Óscar López