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"));
與getClassLoader()方法有關(guān)的問題:為什么下面的代碼中會發(fā)生空指針異常
繁華開滿天機(jī)
2019-01-19 18:15:46