getResourceAsStream()为属性文件返回null

时间:2014-11-11 17:59:45

标签: java resources classloader

我尝试在包config.properties中加载名为config的文件。

My project setup

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);

然而,这给了我一个NullPointerException。但是当我加载img/background/background.png时,我使用:(来自Panel.java

background = new ImageIcon(this.getClass().getClassLoader().getResource("img/background/background.png")).getImage();

这很好用。我已经阅读了很多关于stackoverflow的问题,但找不到我的问题的解决方案。我没有看到加载背景图像或属性文件之间的区别,除了在静态上下文中加载属性文件的事实。但据我所知,这应该有效。

我忘了什么?

编辑:我刚刚运行了System.out.println(Main.class.getClassLoader().getResource("config/config.properties").toString());,它打印了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 - 第70行:

int maxFPS = Integer.getInteger(properties.getProperty("FPS"));

config.properties

FPS=45
fpsCap=1

2 个答案:

答案 0 :(得分:1)

问题不在于getResourceAsStream("config/config.properties"),而在于我阅读属性的方式。属性FPS写为String,要在int中使用Integer.parseInt(),而不是Integer.getInteger()

答案 1 :(得分:0)

尝试从当前线程上下文加载器中获取它:

    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.");
    }

此代码编译为JDK 7+,因为它使用auto可关闭,但可以通过将资源加载器语句移动到try-catch子句中来转换为以前的JDK版本。