# When image is loaded do something

**URL:** https://discourse.processing.org/t/when-image-is-loaded-do-something/4946
**Category:** Coding Questions
**Created:** [October 28, 2018, 11:30am UTC](https://discourse.processing.org/t/when-image-is-loaded-do-something/4946 "2018-10-28T11:30:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)
#### Post date: [October 28, 2018, 11:30am UTC](https://discourse.processing.org/t/when-image-is-loaded-do-something/4946/1 "2018-10-28T11:30:17Z")

</div>

Hi I need some help with an “if” line.  
i want to know if i can so something like this:

```auto
if(image is loaded)
{
 b = true;
 } else {
 b = false;
}

if (b==true)
{
 statement
}

if (b== false)
{
 statement
}

```

the problem is i don’t know how to write “if a picture is loaded”

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 28, 2018, 12:48pm UTC](https://discourse.processing.org/t/when-image-is-loaded-do-something/4946/2 "2018-10-28T12:48:25Z")

</div>

When you are loading your picture, just add a boolean behind the statement that goes like isLoaded = true; and then use that as variable or use the variable with you picture, lets say it is img and do something like if (img != null) {}

And please don’t move the topics back to the wrong cathegory, after someone (only people with Trust level 3 can move others topics) moved your question to the right cathegory.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 28, 2018, 2:35pm UTC](https://discourse.processing.org/t/when-image-is-loaded-do-something/4946/3 "2018-10-28T14:35:27Z")

</div>

One method is to set the image to null, then check if it is null after each loadImage operation.

```auto
PImage img = null;

img = loadImage("DoesNotExist.png");

if(img!=null){
  println("miracle image");
} else {
  println("unloaded image");
}

img = loadImage("https://processing.org/img/processing3-logo.png");

if(img!=null){
  println("expected image");
} else {
  println("website down!");
}

```

Output:

> _The file “DoesNotExist.png” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable._
> 
> unloaded image  
> expected image
