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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马_位志国 中级黑马   /  2013-4-2 21:54  /  1553 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黑马_位志国 于 2013-4-2 22:38 编辑

import java.io.*;
import java.util.*;
//通过反射加载配置文件
public class ReflectTest2 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub

                InputStream in = null;
                try {
                        //in = new FileInputStream("config.properties");//此文件存放在该project下。
                        in = ReflectTest2.class.getClassLoader().getResourceAsStream("config.properties");
                        Properties props = new Properties();
                        props.load(in);
                        String className = props.getProperty("className");
                        Collection<ReflectPoint> collections = (Collection)Class.forName(className).newInstance();
                        
                        //Collection collections = new HashSet();
                        ReflectPoint rp1 = new ReflectPoint(3, 3);
                        ReflectPoint rp2 = new ReflectPoint(5, 5);
                        ReflectPoint rp3 = new ReflectPoint(3, 3);
                        
                        collections.add(rp1);
                        collections.add(rp2);
                        collections.add(rp3);
                        collections.add(rp1);
               
                        System.out.println(collections.size());
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }finally{
                        try {
                                if(in != null)
                                        in.close();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }
        
}

config.properties文件内容是:
className = java.util.HashSet

为什么运行该类会发生如下异常:
Exception in thread "main" java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Unknown Source)
        at java.util.Properties.load0(Unknown Source)
        at java.util.Properties.load(Unknown Source)
        at com.ijunfu.study.java.enhance.day01.ReflectTest2.main(ReflectTest2.java:19)

评分

参与人数 1技术分 +1 收起 理由
张熙韬 + 1

查看全部评分

2 个回复

倒序浏览
in = ReflectTest2.class.getClassLoader().getResourceAsStream("src/config.properties");
这里将文件放到src目录下试试!
回复 使用道具 举报
朱德帅 发表于 2013-4-2 22:23
in = ReflectTest2.class.getClassLoader().getResourceAsStream("src/config.properties");
这里将文件放 ...

我试了一下:
将文件放在src目录下,不用改路径。
in = ReflectTest2.class.getClassLoader().getResourceAsStream("config.properties");

多谢指教!!!:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马