0

So right now I´m trying to load an Image on my Harddrive in a BufferdImage in my Code. Yet I think I did everything right but my trycatch only leaves the catch.

Code for better understanding :

private static BufferedImage image;

public void initPictures() {
    try {
        image = ImageIO.read(new File("Pictures/blue.png"));
    } catch (IOException ex) {
        System.out.println("Will not load");
        ex.printStackTrace();
    }

}

The initPictures() is called in my Constructor of the Class. And you can see here that my picture that I try to load is in the E:\Dropbox\Dropbox\Java Projekte\FallingBlocks1\build\classes\Pictures folder on my OS. So the "Pictures/blue.png" should be good.

Windows Folder Picture:

enter image description here

So my Qustion here is : What am I missing?

EDIT : added ex.printStackTrace(); nothing changed thought.

EDIT : also tried to load other pictures int the "image", with no other results

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
Gwitninm
  • 15
  • 5
  • try with the full file-path to the image file i.e. `(new File(Dropbox//JavaProjekte//FallingBlocks1//build//classes//Pictures//blue.png));` – smoggers Oct 20 '16 at 17:39
  • What does the exception say for itself? What is its message? – nasukkin Oct 20 '16 at 17:39
  • I already tried it with the full path name; but sadly it didn´t help. – Gwitninm Oct 20 '16 at 17:41
  • I don´t get an Exception. I only get a message in the System.Output that says "Will not load". – Gwitninm Oct 20 '16 at 17:42
  • Add an `ex.printStackTrace()` in the `catch` block (at least). – Marco13 Oct 20 '16 at 17:43
  • (BTW: The `blue.png` is wrong. It's darker at the top, whereas all others are brighter at the top). (EDIT: So that's what "SSD" stands for. Never knew that...) – Marco13 Oct 20 '16 at 17:44
  • Of course, the `ex.printStackTrace()` will **NOT** make it work, but when you start it, it should print the stack trace! – Marco13 Oct 20 '16 at 17:46
  • Don´t know if I understand you right, but my Output on the System.Output is still only the "Will not load" text. In addition I tried to load the "green.png" (maybe my picture is broken), but it had no effect at all. – Gwitninm Oct 20 '16 at 17:50
  • An unrelated side note: These images are trivial, and (although it may slightly depend on the use case), I would **strongly** recommend you to draw these images manually. You could have a `void drawBlock(Graphics2D g, int x, int y, int w, int h, Color color)` method with (relatively) few lines of code, where you could draw such blocks with **arbitrary** sizes and **arbitrary** colors. – Marco13 Oct 20 '16 at 18:51

1 Answers1

0

There are two possibilities:

1. Your program may not be running in the folder that contains the "Pictures" folder.

To find out where your program is running, do a System.out.println(System.getProperty("user.dir")); to see what folder you're running in. If it prints something other than E:\Dropbox\Dropbox\Java Projekte\FallingBlocks1\build\classes, you know it's an issue with the path you're providing to ImageIO.read(file). For kicks, you may also want to see what new File("Pictures/blue.png").isFile() is returning.

2. Your image might not readable.

If you try the first test out and you know for certain that the path you are providing is correct, then it's likely that your image file is either corrupt or cannot be read (could be caused by access restrictions, connectivity problems, hardware issues, etc.). Try pointing to a different image and see what happens.

CodeBlind
  • 4,519
  • 1
  • 24
  • 36