This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Null check on an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null check on an image" Watch "Null check on an image" New topic
Author

Null check on an image

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am reading an image directly from url. I am using this image in web service. Now if the image is not available my service breaks and if the image is available my service is working fine. I am wondering if there is a way I can check if the image is exists or not. I would appreciate if someone can help me to solve this issue.

Here is my sample code How I read the image:

Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 82
sorry if this is too basic and I misunderstood the question, but couldn't lines 2-7 simply be wrapped in:

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I tried that before I posted my request. It does not work that way. Thanks for your idea, anyway.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Sam Saha wrote:Now if the image is not available my service breaks

According to the javadocs, ImageIO.read throws an IOException if there's a problem reading from the URL. If you catch that you can execute whatever alternative code is appropriate.
Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 82
not sure what kind of try/catch your code is surrounded with, but I was able to get this to work:



when the bad url is uncommented, it throws an IO exception and just skips the null check...so maybe you just need code to deal with a non-existent image in the catch block.
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
well yes Lester Burnham was right.
Sometimes it appeared to be exception once we hit the broken URL.
or we may say the URL is not valid.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Null check on an image
 
Similar Threads
Fetching an image from the specified URL and resizing it.
java.lang.IllegalArgumentException: im == null!
ImageIO.read() timeout?
Writing Buffered Image quality
Image Handling in Java