Reputation: 125
I have problem with displaying images in the GridPane. I got error that Input stream must not be null. Exception is in first row of the creating object of ImageView. As you can see pictures are in folder. What can be a problem.
ImageView windows = new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
ImageView linux = new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
ImageView mac = new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
ImageView android= new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
GridPane gpanel = new GridPane();
gpanel.setPadding(new Insets(5));
gpanel.add(windows, 0, 0);
gpanel.add(linux, 1, 0);
gpanel.add(mac, 0, 1);
gpanel.add(android, 1, 1);
Upvotes: 1
Views: 492
Reputation: 14102
That's because it's not finding the images, you need to remove the res
at the beginning of the path, so for example:
This: "res/windows.png"
-> Should be: "/windows.png"
...and so on.
Because getResourceAsStream(String name)
finds a resource with a given name via searching resources associated with the class and it starts from the root of your project but because res
is not another package, so you don't need to add it to the beginning of the relative path.
Furthermore, suppose that you have the windows.png
in another package called anotherPackage
, then you can access it from your main
Class (in your example) like this: "/anotherPackage/windows.png"
.
Upvotes: 3