0
Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
//at InputStreamReader inStream = new InputStreamReader(fis);

Also, should I add throws IOException, FileNotFoundException to main or use try{} instead?

    System.out.print("Enter the filename: ");

    Scanner stdin = new Scanner(System.in);  //Keyboard input
    String fileName=stdin.nextLine();

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(fileName);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
    InputStreamReader inStream = new InputStreamReader(fis);
    BufferedReader in = new BufferedReader(inStream);
5
  • So your question is what went wrong and/or what should you do about it? Commented Mar 1, 2012 at 3:21
  • I think answers to both would be helpful. Commented Mar 1, 2012 at 3:24
  • 1
    Looks like your file input stream is null. You should move the bottom two lines of your code into the try block. And as a general practice its a bad idea to swallow exceptions. Commented Mar 1, 2012 at 3:25
  • When the two lines were in try block, "in" became undefined later on. Should I change that to throws FileNotFoundException? Could you clarify "swallow" Commented Mar 1, 2012 at 3:27
  • swallow means to catch an exception and ignore it. Although you are printing out the stack trace (better than nothing), the program still continues to execute without having fixed the problem. From the program's perspective, you have ignored the exception, hence swallowing. Generally speaking, either fix it, or let the program terminate. Commented Jan 25, 2013 at 21:51

2 Answers 2

5

You've made the classic mistake of catching the exception (in this case FileNotFoundException) and not actually recovering from it. So when the file open fails, you are then passing a null argument to InputStreamReader(...), and that is causing the NPE.

Also, should I add throws IOException, FileNotFoundException to main or use try{} instead?

That depends on your requirements. You have to decide whether you want to let the exceptions to propagate to main (which will probably have to give up), or whether you want the current method to attempt to recover. For instance, you could ask for a different filename ...

Sign up to request clarification or add additional context in comments.

Comments

-3

The code works. Just tested it myself. The file name you're entering must not be there.

Incidentally, since you're already using a Scanner to read from stdin, you should also use a Scanner to reader your file. I think BufferedReaders are bit clunky to work with.

2 Comments

Maybe clunky was the wrong word to use. What I really meant was the Scanner provide more utilities than BufferedReaders do. Trying to anything other than readLine() with BufferedReader means you have to start doing things one char at a time. With all the utilities a Scanner provides, it's easier to work with and do more.
But the code still works just under certain conditions. The file must exist at the specified location and the string must be correct. If not, the exception is thrown, printed, and the code continues running without handling the exception. So the code doesn't really work. The BufferedReader is the least of it's problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.