java.lang.NullPointerException at java.io.Reader.<init>(Reader.java:61) at java.io.InputStreamReader.<init>(InputStreamReader.java:80) at java.util.Properties.load(Properties.java:189) at java.util.PropertyResourceBundle.<init>(PropertyResourceBundle.java:96) at encry.ReadIniFile.ReadFile(ReadIniFile.java:21) at encry.ReadIniFile.main(ReadIniFile.java:49)Exception in thread "main" wojue() :什么意思,怎么改???
wojue() :什么意思,怎么改???
是这句的问题拉
你classloader.getResourceAsStream("D://ConnectionPool.ini")返回的是一个为null的InputStream对象,所以会造成空指针错误……
import java.io.*;
public class t021216 { private static Hashtable has = new Hashtable();
static String head = "#";
public t021216() {
}
public void ReadFile() throws Exception {
System.out.println("before");
ClassLoader classloader = getClass().getClassLoader();
System.out.println("after");
PropertyResourceBundle resource =new PropertyResourceBundle(classloader.getResourceAsStream("D:/ConnectionPool.ini"));
//InputStream resource = getClass().getResourceAsStream("D://ConnectionPool.ini");
Enumeration enu = resource.getKeys();
while (enu.hasMoreElements()) {
String alias = (String) enu.nextElement();
if (alias.endsWith("jdbcDriver")) {
// String str="jdbcDriver";
puthashtable(alias, "jdbcDriver", resource);
}
}
}
private void puthashtable(String alias,String str,PropertyResourceBundle resource)
throws Exception {
//read from resource
String connectalias = alias.substring(0, alias.length() - str.length());
if (has.get(head + connectalias) == null) {
String jdbcDriver = resource.getString(connectalias + "jdbcDriver");
String jdbcURI = resource.getString(connectalias + "jdbcURI");
String dbusername = resource.getString(connectalias + "dbusername");
String dbpassword = resource.getString(connectalias + "dbpassword");
System.out.println(jdbcDriver+""+jdbcURI+""+dbusername+""+dbpassword+"");
}
} public static void main(String[] args) throws Exception {
t021216 readIniFile1 = new t021216();
readIniFile1.ReadFile();
}
ClassLoader classloader = getClass().getClassLoader();
System.out.println("Hello");
PropertyResourceBundle resource =new PropertyResourceBundle(classloader.getResourceAsStream("D://ConnectionPool.ini"));
System.out.println("bye");
然后,就知道,是那句话出现问题了的!
Returns:an input stream for reading the resource, or null if the resource could not be found
改动部分:
InputStream is=getClass().getResourceAsStream("D://ConnectionPool.ini");
PropertyResourceBundle resource =new PropertyResourceBundle(is);不要这句
ClassLoader classloader = getClass().getClassLoader();