Java getResourceAsStream()为属性文件返回null

Java getResourceAsStream()为属性文件返回null,java,resources,classloader,Java,Resources,Classloader,我尝试在包config中加载名为config.properties的文件 Main.java中的代码片段: //Read config.properties Properties properties = new Properties(); System.out.println(Main.class.getClassLoader().getResource("config/config.properties").toString());

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

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时,我使用:(from
Panel.java

这个很好用。我已经读了很多关于stackoverflow的问题,但是找不到解决问题的方法。除了在静态上下文中加载属性文件之外,我看不到加载背景图像和加载属性文件之间的区别。但在我看来,这应该行得通

我忘了什么

编辑:我刚刚运行了
System.out.println(Main.class.getClassLoader().getResource(“config/config.properties”).toString())
,它将正确的路径打印到
config.properties

Stacktrace:
java.lang.NullPointerException
在main.main.startGame(main.java:70)
在main.gui.panel.MenuPanel$1.actionPerformed(MenuPanel.java:31)
在javax.swing.AbstractButton.fireActionPerformed(未知源)
位于javax.swing.AbstractButton$Handler.actionPerformed(未知源)
在javax.swing.DefaultButtonModel.fireActionPerformed(未知源)
位于javax.swing.DefaultButtonModel.setPressed(未知源)
位于javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源代码)
位于java.awt.Component.ProcessMouseeEvent(未知源)
位于javax.swing.JComponent.ProcessMouseeEvent(未知源)
位于java.awt.Component.processEvent(未知源)
位于java.awt.Container.processEvent(未知源)
位于java.awt.Component.dispatchEventImpl(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.LightweightDispatcher.RetargetMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.ProcessMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.dispatchEvent(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Window.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.EventQueue.dispatchEventImpl(未知源)
位于java.awt.EventQueue.access$400(未知源)
在java.awt.EventQueue$3.run处(未知源)
在java.awt.EventQueue$3.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
在java.awt.EventQueue$4.run处(未知源)
在java.awt.EventQueue$4.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
位于java.awt.EventQueue.dispatchEvent(未知源)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
位于java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
位于java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
在java.awt.EventDispatchThread.run(未知源代码)

Main.java
-第70行:

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

FPS=45
fpsCap=1

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

    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 closeable,但是可以通过将resource loader语句移动到try catch子句中来转换为以前的JDK版本。

问题不在于
getResourceAsStream(“config/config.properties”)
,而在于我读取属性的方式。属性
FPS
被写为
字符串
,要在
int
中实现这一点,我必须使用
Integer.parseInt()
,而不是
Integer.getInteger()

与Main.java哪个类不同?@SotiriosDelimanolis我将更新图片并添加它。在上面。@SotiriosDelimanolis再次看到问题。介意发布stacktrace吗?另外,显示在何处声明
属性
properties
可以为空,并且调用
properties.load
可能是NPE正在通过的位置。注意:
Integer#getInteger(..)
不是合适的使用方法。读这本书。您可能需要
parseInt
,这不应该是
properties.load(in)?是的,关于applicationProperties->properties加载,您是对的,已经编辑了代码示例。在示例代码中添加了用于查找属性文件的配置/路径。
    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.");
    }