gpt4 book ai didi

java - getResourceAsStream() 为属性文件返回 null

转载 作者:行者123 更新时间:2023-11-30 11:12:30 25 4
gpt4 key购买 nike

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

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 的正确路径。

堆栈跟踪: 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.processMouseEvent(未知来源)
在 javax.swing.JComponent.processMouseEvent(未知来源)
在 java.awt.Component.processEvent(未知来源)
在 java.awt.Container.processEvent(未知来源)
在 java.awt.Component.dispatchEventImpl(未知来源)
在 java.awt.Container.dispatchEventImpl(未知来源)
在 java.awt.Component.dispatchEvent(未知来源)
在 java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)
在 java.awt.LightweightDispatcher.processMouseEvent(未知来源)
在 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( native 方法)
在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源)
在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源)
在 java.awt.EventQueue$4.run(未知来源)
在 java.awt.EventQueue$4.run(未知来源)
在 java.security.AccessController.doPrivileged( native 方法)
在 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

最佳答案

问题不在于 getResourceAsStream("config/config.properties"),而在于我读取属性的方式。属性 FPS 被写成一个 String,为了在 int 中实现它,我不得不使用 Integer.parseInt(),而不是 Integer.getInteger()

关于java - getResourceAsStream() 为属性文件返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26871358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com