Null Pointer Exception for read properties file in Idea

喜欢而已 提交于 2019-12-24 15:25:31

问题


after define a property file in idea 10.5 when i try to use it,compiler show me Null Pointer Exception!I try anything that i think fix it among change property file path,... here is the code that i write: thanks. beginner programmer!!!

public class MainDlg{
    Properties properties=new Properties();
    public MainDlg() {
        try {
            InputStream reader=MainDlg.class.getResourceAsStream("propFile");
            properties.load(reader);

回答1:


Sounds like the reader is null. Make sure the path is correct.

If property file is the root then you should

InputStream reader=MainDlg.class.getResourceAsStream("/propFile");



回答2:


getResourceAsStream returns null if the resource cannot be found. Are you positive that, relative to the location of the MainDlg.class file, there is a folder called presentation, in which is a file called propFile?




回答3:


getResourceAsStream is case sensitive. Make sure that your file-name matches exactly.




回答4:


Where did you put the propFile? It should reside in the Source root, however IDEA will not copy it by default to the classpath as there is no pattern for the empty extension in Settings | Compiler | Resource Patterns.

It's recommended that you rename the file to something.properties so that IDEA copies it to the output (classpath) and it will become available as a classpath resource.

If you don't want to rename the file, put it in some directory outside the source root and configure this directory as a library dependency in IDEA Project Structure | Modules | Dependencies, this way it will be available from classpath.




回答5:


probably you resources (prop files) are not put into the classes directory. In any case here is a tip ;) just before reading the property, print the following:

new java.util.File(".").getAbsolutePath()

This should give you a good idea of what is your actual current directory Hope this will resolve your issues. Good luck!



来源:https://stackoverflow.com/questions/7101484/null-pointer-exception-for-read-properties-file-in-idea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!