java - FileInputStream 抛出 NullPointerException

标签 java nullpointerexception fileinputstream

我收到 nullpointerException,不知道到底是什么原因导致的。我从java文档中读到fileinputstream只抛出securityException所以不明白为什么会弹出这个异常。 这是我的代码片段。

private Properties prop = new Properties();
private String settings_file_name = "settings.properties";
private String settings_dir = "\\.autograder\\";

public Properties get_settings() {
    String path = this.get_settings_directory();
    System.out.println(path + this.settings_dir + this.settings_file_name);
    if (this.settings_exist(path)) {
        try {
            FileInputStream in = new FileInputStream(path + this.settings_dir + this.settings_file_name);
            this.prop.load(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        this.create_settings_file(path);
        try{
            this.prop.load(new FileInputStream(path + this.settings_dir + this.settings_file_name));
        }catch (IOException ex){
            //ex.printStackTrace();
        }
    }
    return this.prop;
}

private String get_settings_directory() {
    String user_home = System.getProperty("user.home");
    if (user_home == null) {
        throw new IllegalStateException("user.home==null");
    }

    return user_home;
}

这是我的堆栈跟踪:

C:\Users\mohamed\.autograder\settings.properties
Exception in thread "main" java.lang.NullPointerException
        at autograder.Settings.get_settings(Settings.java:41)
        at autograder.Application.start(Application.java:20)
        at autograder.Main.main(Main.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Line 41 is: this.prop.load(in);

最佳答案

如果第 41 行是 this.prop.load(in); 那么看起来好像 this.prop == null

在该行添加断点进行验证。

尝试在 null 实例上调用方法会导致 NullPointerException

关于java - FileInputStream 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2649073/

相关文章:

java - 最终如何阻止工作

java - 如果使用 @Nonnull 注释,则不会出现针对 null 返回值的 FindBugs 警告

java - NullPointerException 我无法解决

java - 无法在文件中找到换行符(java)

java - NoClassDefFoundError org/eclipse/swt/SWTError

java - 哪些字符串是空的?

java - ReflectionUtil getField 抛出 NullPointerException

java - 我应该在使用 throw 的同时使用 throws 吗?

primefaces - 第二次单击primefaces下载时读取错误

java - 运行时优化