Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the following code retain the null value of in? Basically I wanted to wrap the code around try...catch block to ensure that "database.properties" exists where the IDE is looking for it..

 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an Exception is thrown in the try, the value of in will remain null.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If an Exception is thrown in the try, the value of in will remain null.


Thanks, hence the appropriate exception handler must be written.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've already got the Exception handler. You can now proceed with if (in != null) ... tests.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You've already got the Exception handler. You can now proceed with if (in != null) ... tests.


That's a good point, I was just thinking that since my block of statements handles FileNotFoundException, I would add another catch statement that catches all other exceptions:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be better to catch an IOException there, rather than declaring it.

There is another way of opening streams which Rob Spoor showed me a few weeks ago, which avoids the need for null checks, but I can't remember where it is. It's somewhere on the Ranch.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do that but that would not solve the problem, since the code that is in the try-block will not throw any other checked exceptions than IOException, and when it throws an unchecked exception you probably wouldn't want to catch it anyway. The code after the catch blocks would still be reached with in being null.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The != null check you showed is in the wrong place; it will actually make things worse there. You initialise the Stream inside a try; you use the != null check afterwards. If you have all the reading done inside the same try, you only need the != null check when you close the stream.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic