In a Java project within NetBeans 7.0, retrieving an image from the "resources" folder using the following code results in a NullPointerException:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
Despite confirming the presence of the image in the resources folder, this approach fails.
1. Folder Structure:
Separate the resource folder from the src folder to avoid deletion during build:
2. getResource(Path):
In Java, resources are accessed relative to the root of the classpath, which in this case is the "classes" folder within the build folder:
3. Example:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("/resources/images/filling.jpg"));
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
4. Build Folder:
5. Troubleshooting:
The above is the detailed content of How to Avoid NullPointerException when Retrieving Images from Resources Folder in NetBeans?. For more information, please follow other related articles on the PHP Chinese website!