Array of Images Error (loading of images)

Whenever I write this code:

PImage[] flowers=new PImage[3];
Bubble[] bubbles=new Bubble[5];

void setup() {
size(640, 360);
for(int i=0; i<flowers.length; i++){
flowers[i]=loadImage(“flower”+i+".jpg");
}
for(int i=0; i<bubbles.length; i++){
int index=int(random(0,flowers.length));
bubbles[i]=new Bubble(flowers[index],100+i*100, 300, random(32, 72));
}
}

void draw() {
background(255);
for (int i=0; i<bubbles.length; i++){
bubbles[i].display();
bubbles[i].ascend();
bubbles[i].top();
}
}

AND CLASS:
class Bubble {
float x;
float y;
float diameter;
PImage img;

Bubble(PImage tempImg, float tempX, float tempY, float tempD) {
x=tempX;
y=tempY;
diameter=tempD;
img=tempImg;
}
void display() {
stroke(0);
fill(127);
image(img,x, y, diameter, diameter);
}
void ascend() {
y–;
x=x+random(-2, 2);
}
void top() {
if (y<diameter/2) {
y=diameter/2;
}
}
}

I ALWAYS get errors saying ‘‘The file “flower0.jpg” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
The file “flower1.jpg” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
The file “flower2.jpg” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
NullPointerException
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting.’’

Even though I have doubled checked on how and where I saved the images (name etc) I still can’t get past this.

Hi,

Welcome to the community! :slight_smile:

If your project is named sketch, you should have the following folder structure :

sketch
├── data
│   ├── flower0.jpg
│   ├── flower1.jpg
│   └── flower2.jpg
└── sketch.pde

1 directory, 4 files

Your images need to be in a data folder inside your sketch folder, then it should work.

If you are on Linux or Mac, can you use the tree command in the terminal (used above) to show the file structure of your project?

1 Like

Also, it’s case sensitive, so jpg not equal JPG

your program works for me, I just run it

1 Like

All I see is a file names sketch and inside that file there is the file with the processing code, another processing code file that exists because I wrote the class on a different tab inside processing and nothing else. There is no data folder. I made a folder inside that folder and put my images inside and it still doesn’t work.

I tried it with .JPG and .jpg and none of it works

You must copy the images in your Sketch folder

I did that and it won’t work

Do you have maybe F or f flower?

what does this print please:




void setup() {
  size(770, 770);
  PImage[] list = loadImages();
  println (list.length);
  image(list[0], 0, 0);
}

PImage[] loadImages() {
  java.io.File folder = new java.io.File(dataPath("")+"/");
  String[] filenames = folder.list();
  PImage[] img = new PImage[filenames.length];
  for (int i = 0; i < filenames.length; i++) {
    println(dataPath("") + "/" + filenames[i]); 
    img[i] = loadImage(dataPath("") + "/" + filenames[i]);
  }
  return img;
}

If I run what you wrote it highlights PImage[] img = new PImage[filenames.length];
and gives me NullPointerException error.

are you sure you have a folder data in your Sketch’s folder???

What happens when you press ctrl-k in your Sketch?

the folder in which the sketch and the folder data (with the images) are opens up

1 Like

then the example Sketch should show something

Did you write “data” with small letters only?

void setup() {
  size(770, 770);

  PImage[] list = loadImages();
  println (list.length);

  if (list.length>0) 
    image(list[0], 0, 0);
  else  println ("no image found");
}

PImage[] loadImages() {
  java.io.File folder = new java.io.File(dataPath("")+"/");
  String[] filenames = folder.list();

  PImage[] img = new PImage[filenames.length];
  for (int i = 0; i < filenames.length; i++) {
    println(dataPath("") + "/" + filenames[i]); 
    img[i] = loadImage(dataPath("") + "/" + filenames[i]);
  }
  return img;
}

Weird. It runs on my computer.

Are you on Win 10 ?

Besides, I can run your initial Sketch with images even without the data folder,
when the 3 images just are in your Sketch folder.

Ok so the second code you wrote runs just fine and the corresponding image appears. And yes i’m on win 10 and the file name is data small letters

ok, so the second code runs with the images flower0, flowers1 and flowers2

The same images from your initial Sketch / game?

What does this print please?



void setup() {
  size(770, 770);

  PImage[] list = loadImages();
  println (list.length);

  if (list.length>0) 
    image(list[0], 0, 0);
  else {
    println ("no image found");
  }
}

PImage[] loadImages() {
  java.io.File folder = new java.io.File(dataPath("")+"/");
  String[] filenames = folder.list();

  PImage[] img = new PImage[filenames.length];
  for (int i = 0; i < filenames.length; i++) {
    println(dataPath("") + "/" + filenames[i]); 
    img[i] = loadImage(dataPath("") + "/" + filenames[i]);
  }
  return img;
}

Yes the correspoding images appears (same as the ones I was trying to use before) and this message prints C:\Users\USER\AppData\Local\Temp\untitled4877301069392334631sketches\sketch_210104a\data/flower0.jpg.jpeg
C:\Users\USER\AppData\Local\Temp\untitled4877301069392334631sketches\sketch_210104a\data/flower1.jpg.jpg
C:\Users\USER\AppData\Local\Temp\untitled4877301069392334631sketches\sketch_210104a\data/flower2.jpg.jpg
3

Thanks

What’s wrong with the file name here?

Please rename 3 files and try initial Sketch again.

wait so it should be flower0.jpg.jpg instead of flower0.jpg? did i understand correctly?