0

So here's my problem: I need to instantiate Images inside a loop, but the URL doesn't work, it keeps throwing the 'url not found' error...

That's my code :

for (int k=0; k<=10; k++){
    String nb=String.valueOf(k);
    nombres[k]= new Image(getClass().getResourceAsStream("resource/"+nb+".png"));
    boutons[k]= new Button();
    boutons[k].setGraphic(new ImageView(nombres[k]));
}

I'm waiting for your advice?

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Lucille
  • 11
  • 1
  • 4
  • 4
    this might enlighten you https://stackoverflow.com/questions/861500/url-to-load-resources-from-the-classpath-in-java look through different answer, it covers getResourceAsStream – Maytham Fahmi Dec 26 '17 at 18:12
  • 2
    Post your project structure: this is going to expect a subpackage called `resource` in the package containing the class with the code you posted. – James_D Dec 26 '17 at 18:19
  • 2
    Also, you really don't need a stream here: use `getResource()` not `getResourceAsStream()`. You still need to ensure the path is correct, though. – James_D Dec 26 '17 at 18:23
  • When I write "resource/1.png" it works perfectly fine. I tried a print on nb to see if there was any error and there wasn't anything wrong. – Lucille Dec 28 '17 at 08:42
  • So does it work for all the values if you hard-code it? Which value of `k` actually causes the exception? – James_D Dec 29 '17 at 04:35

0 Answers0