# loadImage only if file exist

**URL:** <https://discourse.processing.org/t/loadimage-only-if-file-exist/33929>\
**Category:** Beginners\
**Created:** [December 4, 2021, 8:31pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929 "2021-12-04T20:31:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rizn](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rizn](https://discourse.processing.org/u/rizn)\
**Post date:** [December 4, 2021, 8:31pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929/1 "2021-12-04T20:31:47Z")

</div>

hi.  
i use loadImage() for a sketch, to read a lot of images from web and export it to pdf.  
its working fine.  
Is there a way to ask, if the image-file exist before using loadImage()?  
In my case, if some files dont exist, the pdf-exporter stops rendering with a error.

Thank you!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 4, 2021, 8:42pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929/2 "2021-12-04T20:42:46Z")

</div>

Hello,

This works:

> **[How to check if file exists in Java - HowToDoInJava](https://howtodoinjava.com/java/io/how-to-check-if-file-exists-in-java/)**
>
> Learn how can we test to check if file exists or a directory exist in given path in Java. Also check is file is readable, writable or executable.

```auto
void setup() 
  {
  File tempFile = new File("c:/test/test.txt");
  boolean exists = tempFile.exists();
  println(exists);
  }

```

`:)`

---

<div class="post-metadata">

**Author:** ![rizn](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rizn](https://discourse.processing.org/u/rizn)\
**Post date:** [December 4, 2021, 8:59pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929/3 "2021-12-04T20:59:12Z")

</div>

thank you very much for your quick answer.  
is there a way to test “on the fly”?  
i have a for-to-loop (10000 urls) and im looking for something like:

for-to-loop {  
if (file\_exist(url)){  
loadImage(url)  
}  
}

Background:  
My images are named like image1, 2, 3, 4, 5, 6, 7… image10000.jpg!  
But just few files are missing.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 4, 2021, 9:30pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929/4 "2021-12-04T21:30:26Z")

</div>

> [@rizn](#):
>
> Is there a way to ask, if the image-file exist before using **loadImage()**?

According to **loadImage()**'s reference:

> **[loadImage() / Reference](https://processing.org/reference/loadImage_.html)**
>
> Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the …

It simply returns `null` when it fails:

> If the file is not available or an error occurs, `null` will be returned and an error message will be printed to the console. The error message does not halt the program, however the `null` value may cause a NullPointerException if your code does not check whether the value returned is `null`.

Let’s say you’ve got an ArrayList to store all your loaded images:

> **[ArrayList / Reference](https://processing.org/reference/ArrayList.html)**
>
> An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is…

```auto
import java.util.List;
final List<PImage> images = new ArrayList<PImage>();

```

Then you could just check for `null` before **add()** each PImage:

```auto
PImage img;

for (final String url : loadStrings("urls.txt"))
  if ((img = loadImage(url)) != null) images.add(img);

```

> [@rizn](#):
>
> My images are named like image1, 2, 3, 4, 5, 6, 7… image10000.jpg!

If that’s so the previous loop can be adapted like this:

```auto
final String url = "https://SiteName/FolderName/", name = "image", ext = ".jpg";
PImage img;
int i = 0;

while (++i <= 10000)
  if ((img = loadImage(url + name + i + ext)) != null) images.add(img);

```

---

<div class="post-metadata">

**Author:** ![rizn](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rizn](https://discourse.processing.org/u/rizn)\
**Post date:** [December 4, 2021, 9:51pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929/5 "2021-12-04T21:51:02Z")

</div>

thank you very much, i will check this.  
it will help me for sure 😃

---

<div class="post-metadata">

**Author:** ![rizn](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rizn](https://discourse.processing.org/u/rizn)\
**Post date:** [December 5, 2021, 7:34pm UTC](https://discourse.processing.org/t/loadimage-only-if-file-exist/33929/6 "2021-12-05T19:34:57Z")

</div>

it works perfect! thanx again!!! great!
