加载属性文件时出现 Java NullPointerException [英] Java NullPointerException on loading properties file

查看:23
本文介绍了加载属性文件时出现 Java NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class SupplierCalculatorApplet extends JApplet{
...
public void init(){
    loadProperties();
    ...
}   

...

private void loadProperties() {
    language = "en-us";//getParameter("Language");
    prop = new Properties();
            try {
                URL urlToProps = this.getClass().getResource("config/" + language + ".properties");
                prop.load(urlToProps.openStream());//Exception Caught Here
            } catch (IOException e) {
            }   
}

在上面指示的行中发现异常.无论语言是否是有效的属性文件,我都会在同一行捕获相同的异常.

Exception is found at the line indicated above. Whether language is a valid properties file or not, I catch the same exception on the same line.

推荐答案

更改为:

prop.load(this.getClass().getResourceAsStream("/config/" + language + ".properties")); 

这篇关于加载属性文件时出现 Java NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆