How to correctly get image from 'Resources' folder in NetBeans

ShockwaveNN :

I have a Java Project in NetBeans 7.0.

I want to add some image to some label dynamically. The image will differ depending on the state of the program.

I put one such image, 'filling.jpg', in the 'resources' folder of my project.

I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).

So I found this method:

ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);

It keeps give me java.lang.NullPointerException. But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).

What am I doing wrong, and how can I get that image correctly?

ShockwaveNN :

Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image. In one of my tries i use

    String pathToImageSortBy = "resources/testDataIcons/filling.png";
    ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));

But correct way was use name of my project in path to resource

String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How best to access a file from resources folder

How to Correctly Close Resources

JavaFX Image from resources folder

how to get list of all files from folder inside java resources folder

How to get resources folder(files) from other maven module?

How to Properly Get a File from resources Folder

Get folder from Resources folder JAVA

How to get image from resources in Laravel?

How to access resources file (css, js files from theme folder) and get link from FTL?

How to get image file from public folder as a response in Laravel?

Is there a more efficient way to get images from the resources folder than to manually assign each image?

How to correctly set the Uri of an image from a folder selected by the user to show in an ImageView?

Get image from resources folder - Laravel

Netbeans, get file properties from res folder

How to change code get image from a folder in github

Error while getting image from resources folder

How to get a variable image in richfaces from resources

store image into a folder in netbeans

How to extract an image from resources?

How to get image from assets by folder ios

How to make two different folder get resources from one folder?

In spring-boot app, how to load static content (image for example) into jsp from local folder instead of resources folder?

How to get and save image to folder from listview

Netbeans Designer and .form files in resources folder

How do i get a random image from a folder python

How to correctly GET an image file from server?

JS - how to importe correctly image from public folder

how to get the application folder from the docker image

How to get Color(JetpackCompose) from resources correctly?