Reputation: 373
I try to load a file named config.properties
in the package config
.
A snippet of my code in Main.java
:
//Read config.properties
Properties properties = new Properties();
System.out.println(Main.class.getClassLoader().getResource("config/config.properties").toString());
InputStream propertiesFile = Main.class.getClassLoader().getResourceAsStream("config/config.properties");
properties.load(propertiesFile);
This however gives me a NullPointerException
. But when I load img/background/background.png
I use: (from Panel.java
)
background = new ImageIcon(this.getClass().getClassLoader().getResource("img/background/background.png")).getImage();
This works fine. I've read a lot of questions on stackoverflow already, but can't find a solution to my problem. I do not see the difference between the loading of the background image or the properties file, other than the fact that the properties file is loaded in a static context. But as far as I can see, this should work.
What am I forgetting?
EDIT: I just ran System.out.println(Main.class.getClassLoader().getResource("config/config.properties").toString());
, which printed the correct path to config.properties
.
Stacktrace: java.lang.NullPointerException
at main.Main.startGame(Main.java:70)
at main.gui.panel.MenuPanel$1.actionPerformed(MenuPanel.java:31)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Main.java
- line 70:
int maxFPS = Integer.getInteger(properties.getProperty("FPS"));
config.properties
:
FPS=45
fpsCap=1
Upvotes: 2
Views: 3259
Reputation: 373
The problem wasn't with the getResourceAsStream("config/config.properties")
, but with the way I read the properties. Property FPS
is written as a String
, to make this in an int
I had to use Integer.parseInt()
, instead of Integer.getInteger()
.
Upvotes: 1
Reputation: 5175
Try getting it from the current thread context loader:
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try (InputStream in = loader.getResourceAsStream("config/config.properties")) {
properties.load(in);
} catch (IOException e) {
throw new IllegalStateException("Cannot start, properties not found.");
}
This code compiles for JDK 7+, since it uses auto closeable, but can be translated to previous JDK version by moving the resource loader statement to be in in the try-catch clause.
Upvotes: 0