Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null check on an image

 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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:

 
Edmund Castermund
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I tried that before I posted my request. It does not work that way. Thanks for your idea, anyway.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • 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: 99
  • Mark post as helpful
  • send pies
  • 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.
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic