Java IntelliJ(添加到类路径)JSP(null指针ex)中的属性文件存在问题
在尝试加载属性文件时,我不断收到空指针异常。这是我第一次尝试这样做,在我的研究中,我似乎需要将它添加到类路径中。以下是我当前用于加载它的代码:Java IntelliJ(添加到类路径)JSP(null指针ex)中的属性文件存在问题,java,jsp,properties,intellij-idea,Java,Jsp,Properties,Intellij Idea,在尝试加载属性文件时,我不断收到空指针异常。这是我第一次尝试这样做,在我的研究中,我似乎需要将它添加到类路径中。以下是我当前用于加载它的代码: //Set up the properties String filePath = "src/prop.properties"; InputStream inputStream = this.getClass().getResourceAsStream(filePath); //Load the properties f
//Set up the properties
String filePath = "src/prop.properties";
InputStream inputStream = this.getClass().getResourceAsStream(filePath);
//Load the properties file
try
{
properties.load(inputStream);
logger.info("Properties file was loaded successfully.");
}
catch(IOException ex)
{
logger.severe("Properties file could not be loaded. Exception : " + ex.getMessage());
}
catch(NullPointerException ex)
{
logger.severe("Null Pointer exception occurred when attempting to load Properties file. Exception : " + ex.getMessage());
}
要测试属性文件,我只需右键单击“我的项目”,然后新建-->文件-->prop.properties
我在这里看不到(我可能看错了地方,老实说我不确定):
如果我理解正确,我的prop.properties文件不应该显示在src文件夹下吗?任何关于如何实现这一点的帮助都将不胜感激,我已经看过了其他类似的帖子,但都无济于事
谢谢。您正在将属性文件放入
src
(一个源代码文件夹),它意味着类路径根包。因此,您应该通过getResourceAsStream(“/prop.properties”)
访问该文件。如果您将属性文件放入src
(一个源代码文件夹),它意味着类路径根包。因此,您应该通过getResourceAsStream(“/prop.properties”)
访问该文件,这就是它所做的一切!非常感谢,这件小事把我的头撞到了墙上。有时候你所需要的只是另一双眼睛,我想。这就是全部!非常感谢,这件小事把我的头撞到了墙上。我想,有时候你需要的只是另一双眼睛。