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();  
         }
     } 
}        

2 Answers 2

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.

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

1 Comment

I see, thank you very much! As soon as I get 15 reps I make sure to upvote your answer. Thanks again!
2

inputstream is still null

Make the following change:

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

or use try-with-resources instead.

1 Comment

I see, thank you very much! As soon as I get 15 reps I make sure to upvote your answer. Thanks again!

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.