在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 與getClassLoader()方法有關(guān)的問題:為什么下面的代碼中會發(fā)生空指針

與getClassLoader()方法有關(guān)的問題:為什么下面的代碼中會發(fā)生空指針異常

import java.io.IOException;
import java.util.Properties;

public class PropertyUtil {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(PropertyUtil.class.getClassLoader().getResourceAsStream("LYZ.properties"));
            String username = properties.getProperty("username");

            System.out.println(username);
        } catch (IOException e) {
            System.out.println("無法讀取LYZ.properties");
        }
    }
}

控制臺信息

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at com.test.PropertyUtil.main(PropertyUtil.java:10)

第十行代碼就是:
properties.load(PropertyUtil.class.getClassLoader().getResourceAsStream("LYZ.properties"));
 
回答
編輯回答
冷眸

應(yīng)該是這里報(bào)指針

PropertyUtil.class.getClassLoader().getResourceAsStream("LYZ.properties")

那么最可能的原因就是資源文件沒有找到. 你要確認(rèn)文件所在路徑,因?yàn)槟憬o的是相對路徑, 即LYZ.properties應(yīng)與PropertyUtil所在包的目錄相同才行.

2017年1月26日 12:28