0

I use getResource() to load the image, but I get nullpointer exception. Also I tried to use css but I get nothing. My code for label:

        Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
        ImageView im = new ImageView(mine);

        Label label = new Label();

        label.setGraphic(im);

And for button:

btn.setStyle("-fx-background-image: url('flag.png')");

This code is located in Painter class.

My file tree:

file tree

Saliery
  • 155
  • 5
  • 16

2 Answers2

0

If you use getClass().getResourceAsStream("bomb3.png") java search in the packetof your class. So if your class is Game, your resource must be in the packet: com.name.minesweeperClasses

sej
  • 46
  • 4
-3
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));

here path of bomb3.png is incorrect Please mention the relative path :- ./resources/boob3.png

  • possible duplicate of https://stackoverflow.com/questions/14739550/difference-between-getclass-getclassloader-getresource-and-getclass-getres – Paul Janssens Nov 28 '19 at 13:42
  • This path is not going to work. "./bomb3.png" is more correct I think – D. Lawrence Nov 28 '19 at 13:42
  • no - please don't post random variations of paths, instead read and understand the api doc of the method and explain (by citing/explaining that doc) why one variant will work while the other doesn't) Hint: your variant is even worse - will never work - than the original (which might work if the resource would be in the top-level package) – kleopatra Nov 28 '19 at 15:21
  • 1
    @D.Lawrence - no, periods are not supported in resolving a resource path – kleopatra Nov 28 '19 at 15:25