gpt4 book ai didi

Java加载属性文件java.lang.NullPointerException错误

转载 作者:行者123 更新时间:2023-11-30 08:32:27 29 4
gpt4 key购买 nike

<分区>

我有一个属性文件,名称是XYZ.properties。我想从此文件中获取详细信息。我的属性文件位置是 D:\properties_file\XYZ.properties

为此我使用下面的代码

import java.io.InputStream;
import java.util.Properties;

public class LoadProp {
private static Properties prop = new Properties();
public static Properties getProperties() throws Exception {
if (prop.isEmpty()) {
InputStream fis = LoadProp.class.getResourceAsStream("D:\\properties_file\\XYZ.properties");
prop.load(fis);
}
return prop;
}
}

public class demo extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Properties propFile;

public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
propFile = LoadProp.getProperties();
System.out.println(propFile.getProperty(Constants.URL));
}
catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

但是当我在 prop.load(fis) 行上运行它时,会出现以下错误

java.lang.NullPointerException

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