gpt4 book ai didi

java - java中发现空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:46:24 24 4
gpt4 key购买 nike

private static Properties getProperties(File file)
{
InputStream in = null;
try
{
in = new FileInputStream(file);
return loadProperties(in);
}
catch (IOException ex)
{
return null;
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException ex)
{
ex.printStackTrace();
// ignore
}
}
}
}

当我单击“保存”按钮保存我的配置时,我收到空指针异常。我尝试调试代码,当时我发现对象in的值为null。我不懂为什么?

我还检查了作为参数文件传递的路径是否正确

最佳答案

我相信发生 NPE 的原因只有一个 - 它没有找到您提供的路径的文件。

I have also checked that the path which I have pass as argument file is correct

有时,即使您认为您提供了正确的文件路径,取决于目录层次结构,程序也可能引用相对路径,该路径可能与您给出的路径不同。然后你就得到了 in = null

尝试ClassLoader.getSystemResourceAsStream()您可以将文件放在类路径下,而不是 FileInputStream()。

关于java - java中发现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24658122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com