3

I am using this link as a reference for creating custom exceptions. For my class practice, if no file is selected or passed in, my custom UnknownFileException should occur, but when I go and run my driver and put in an invalid filename I get a nullpointerexception instead?

My driver which has Adventure adventure = new Adventure(args[0]).

My custom exception:

import java.io.FileNotFoundException;

public class UnknownFileException extends FileNotFoundException {

    public UnknownFileException() {
        super("We couldn't tell what file this is");    
    }

    public UnknownFileException(String message) {
        super(message);  
    }  
}

Code:

public class practice {
    public String[] array;
    public String line;
    public PrintWriter outputStream = null;
    public Scanner inputStream = null;

    public practice(String fileName) throws UnknownFileException {
        array = new String[100];

        try {
            inputStream = new Scanner(new FileReader(fileName));
            line = inputStream.nextLine();
            array[0] = line;

            for (int i = 1; i < array.length; i++) {
                array[i] = inputStream.nextLine();
            }
            outputStream = new PrintWriter(new  
                FileOutputStream("newFile.txt"));
         } catch(UnknownFileException e) {
             System.out.println(e.getMessage());
         } catch (FileNotFoundException e) {
             throw new UnknownFileException(e.getMessage());
         } finally {
             inputStream.close();  
         }
     } 
}        
code
  • 77
  • 6

2 Answers2

2

inputstream is still null

Make the following change:

     } finally {
         if (inputStream != null) {
            inputStream.close();
         }  
     }

or use try-with-resources instead.

ajb
  • 31,309
  • 3
  • 58
  • 84
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • I see, thank you very much! As soon as I get 15 reps I make sure to upvote your answer. Thanks again! – code Mar 26 '15 at 05:04
2

You probably got a stack trace, which should have pointed you to the line throwing the NullPointerException. I'm guessing it was this line:

inputStream.close();  

The problem is that if you put in an invalid file name, new Scanner(new FileReader(fileName)) will throw, and inputStream will never be assigned. Because you have a finally block, however, before it throws your custom exception, it will try to execute the finally. But this gives a NullPointerException because inputStream is null, and that exception takes precedence, I believe (I'd have to check the language rules to make sure of what happens in this case).

Fix your finally block to test whether inputStream is null.

More: It's §14.20.2 of the JLS. This is pretty complicated, but basically if any exception is thrown from the finally block, any earlier exception thrown or caught is discarded.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • I see, thank you very much! As soon as I get 15 reps I make sure to upvote your answer. Thanks again! – code Mar 26 '15 at 05:04