FileInputStream 抛出 NullPointerException

发布于 2024-08-28 16:56:42 字数 1652 浏览 3 评论 0原文

我收到 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);

I am getting nullpointerexception, don't know what actually is causing it. I read from java docs that fileinputstream only throws securityexception so don't understand why this exception pops up.
here is my code snippet.

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;
}

and here is my stacktrace:

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤 2024-09-04 16:56:42

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

在该行上添加一个断点来验证。

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

If line 41 is this.prop.load(in); then it seems as though this.prop == null

Add a breakpoint on the line to verify.

Attempting to call a method on a null instance results in a NullPointerException.

冰魂雪魄 2024-09-04 16:56:42

第41行执行时变量prop是否为null?尝试调试你的程序来检查这一点。例如添加

if(prop == null)
    System.out.println("prop is null");

另外,NullPointerException 是一个未经检查的异常,因此未记录在 Javadoc 中。

Is the variable prop null when it is executing on line 41? Try debugging your program to check this. e.g. add

if(prop == null)
    System.out.println("prop is null");

Also, NullPointerException is an unchecked exception so isn't documented in Javadoc.

白鸥掠海 2024-09-04 16:56:42

我认为其他审稿人在解释你的问题方面做得相当不错。

几点提示:

  1. 我注意到您捕获了某些异常,但没有抛出它们。如果您不抛出异常,那么捕获它们就没有意义。

  2. 其次,为了避免 NPE,您应该在对对象执行任何操作之前始终检查对象是否为 null。

I think the other reviewers did a fair job in explaining your problem.

Couple of pointers:

  1. I noticed that you are catching certain exceptions but not throwing them. If you do not throw the exception then there is no point in catching them.

  2. Secondly, to avoid NPEs you should always check if any of your object is null before executing anything on the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文