Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot find symbol class IOException [duplicate]

Tags:

java

exception

public void populateNotesFromFile()
{
    try{
        BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
        String fileNotes = reader.readLine();

        while(fileNotes != null){
            notes.add(fileNotes);
            fileNotes = reader.readLine();
        }
        reader.close();
    }
    catch (IOException e){
        System.err.println("The desired file " + DEFAULT_NOTES_SAVED + " has problems being read from");
    }
    catch (FileNotFoundException e){
        System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
    }

    //make sure we have one note
    if (notes.size() == 0){
        notes.add("There are no notes stored in your note book");
    }       
}

Whenever i compile the above i get a message saying cannot find symbol class IOException e

can someone tell me how to fix it please :d

thanks

like image 747
user108110 Avatar asked May 16 '09 10:05

user108110


People also ask

What to do if Cannot find symbol in Java?

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 is error Cannot find symbol?

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.

What is Java IO IOException?

java. io. IOException is an exception which programmers use in the code to throw a failure in Input & Output operations. It is a checked exception. The programmer needs to subclass the IOException and should throw the IOException subclass based on the context.


2 Answers

IOException is a class from the java.io package, so in order to use it, you should add an import declaration to your code. import java.io.*; (at the very top of the java file, between the package name and your class declaration)

FileNotFoundException is a IOException. It's a specialisation of IOException. Once you have caught the IOException, the flow of the program will never get to the point of checking for a more specific IOException. Just swap these two, to test for the more specific case first (FileNotFound) and then handle (catch) any other possible IOExceptions.

like image 164
Peter Perháč Avatar answered Oct 23 '22 04:10

Peter Perháč


You need

import java.io;

at the top of your file.

Also, FileNotFoundException needs to be above IOException since it's a subclass of IOException.

like image 24
TheSoftwareJedi Avatar answered Oct 23 '22 03:10

TheSoftwareJedi