A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈红建 中级黑马   /  2012-8-2 08:59  /  2035 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我将config.properties文件放在根目录下,但运行时报:
java.lang.ExceptionInInitializerError
Caused by: 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 com.liuchen.getProperties.<clinit>(getProperties.java:16)
Exception in thread "main"
似乎config.load(in);这里的in是null,但有文件啊,这是怎么回事?谢谢!
已经放在src目录了,但还是报那个错的 InputStream in=getProperties.class.getClassLoader().getResourceAsStream("/config.properties"); 这个也不行啊,这是怎么回事?
  1. public class getProperties {
  2. private static Properties config = null;

  3. static {
  4. InputStream in = getProperties.class.getClassLoader().getResourceAsStream(
  5. "config.properties");
  6. config = new Properties();
  7. try {
  8. config.load(in);
  9. in.close();
  10. } catch (IOException e) {
  11. System.out.println("No AreaPhone.properties defined error");
  12. }
  13. }

  14. // 根据key读取value
  15. public static String readValue(String key) {
  16. // Properties props = new Properties();
  17. try {
  18. String value = config.getProperty(key);
  19. return value;
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. System.err.println("ConfigInfoError" + e.toString());
  23. return null;
  24. }
  25. }

  26. // 读取properties的全部信息
  27. public static void readAllProperties() {
  28. try {

  29. Enumeration en = config.propertyNames();
  30. while (en.hasMoreElements()) {
  31. String key = (String) en.nextElement();
  32. String Property = config.getProperty(key);
  33. System.out.println(key + Property);
  34. }
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. System.err.println("ConfigInfoError" + e.toString());
  38. }
  39. }

  40. public static void main(String args[]) {
  41. // String LaSaPhone=config.getProperty("LaSaPhone");
  42. // System.out.println(LaSaPhone);
  43. // System.out.println(getPhone.readValue("LaSaPhone"));
  44. getProperties.readAllProperties();
  45. }
  46. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨志 + 1 代码不用太多,贴核心代码就可以了! 加油.

查看全部评分

2 个回复

倒序浏览
问题找到了
package cn.itcast.day1;

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

public class getProperties {

private static Properties config = null;



static {

InputStream in = getProperties.class.getResourceAsStream(                        //此处找的是在加载器目录下的配置文件。方法已经封装了加载器的步骤。原来的多了
"config.properties");

config = new Properties();                       

try {
        System.out.println("aa");                                        //加了判断,看看是不是空。。。。。。。
        if(in==null){
                System.out.println("bb");
        }
config.load(in);

in.close();

} catch (IOException e) {

System.out.println("No AreaPhone.properties defined error");

}

}



// 根据key读取value

public static String readValue(String key) {

// Properties props = new Properties();

try {

String value = config.getProperty(key);

return value;

} catch (Exception e) {

e.printStackTrace();

System.err.println("ConfigInfoError" + e.toString());

return null;

}

}



// 读取properties的全部信息

public static void readAllProperties() {

try {



Enumeration en = config.propertyNames();

while (en.hasMoreElements()) {

String key = (String) en.nextElement();

String Property = config.getProperty(key);

System.out.println(key + Property);

}

} catch (Exception e) {

e.printStackTrace();

System.err.println("ConfigInfoError" + e.toString());

}

}



public static void main(String args[]) {

// String LaSaPhone=config.getProperty("LaSaPhone");

// System.out.println(LaSaPhone);

// System.out.println(getPhone.readValue("LaSaPhone"));

getProperties.readAllProperties();

}

}
运行结果:
aa
classNamejava.util.HashSet



评分

参与人数 1技术分 +1 收起 理由
杨志 + 1

查看全部评分

回复 使用道具 举报
陈红建 来自手机 中级黑马 2012-8-2 14:28:48
藤椅
我晕啊。。脑袋疼了。标题求加分啊。这么经典的问题。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马