• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java i/o stream close() how it works?

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I have a question that

for every stream that we create for java i/o we write at the end in finally block



it works

my question is as per syntax in if (is!=null) this means that if is means stream contain some value then close it(!=null)

then how it in my opnion it should be like (in logical way)


if stream contain nothing then close it

if(is==null)
{
is.close();
}

I am confuse about working this line

can any one explain how it works

thanks
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, you have assigned the input stream to the variable "is". If the variable "is" is NULL, that means it does not point to an input stream object. Therefor if you try to call a method on a variable that does not point to an object, you will get a NullPointerException.

This is the reason to test if the variable is NULL before trying to call a method on it. An exception may have been thrown while the input stream was being created so the input stream object was never assigned to the "is" variable.



Hope this helps.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Scott

Great Answars clearly understand
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I told you in one of your other threads, you are better off learning about try with resources. Then you don't need to write all that boilerplate code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic