2013-02-26 57 views
0

当我试图加载我的属性文件时,我总是收到空指针异常。这是我第一次尝试这样做,在我的研究中,似乎我需要将它添加到类路径中。这是我目前使用的加载它在代码:IntelliJ中的属性文件问题(添加到类路径)JSP(空指针前)

//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

enter image description here

而且我也不在这里看到它(我可能会看在完全错误的地方,说实话我不知道):

enter image description here

如果我正确地理解了东西,我的prop.properties文件是不是应该显示在src文件夹下?任何帮助如何实现这一点将非常感激,我看过其他类似的帖子无济于事。

谢谢。

回答

4

您正在将您的属性文件放入src(源代码文件夹),它表示classpath根包。所以你应该通过getResourceAsStream("/prop.properties")访问文件。

+0

这就是这一切!非常感谢,这件微不足道的事让我把头撞在墙上。我猜,有时你需要的是另一双眼睛。 – 2013-02-26 20:10:13