• 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

Null check on an image

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that before I posted my request. It does not work that way. Thanks for your idea, anyway.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic