本帖最后由 周靖轩 于 2014-8-16 16:44 编辑
InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("config.properties")
总是报错啊,试着在config.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 advstudy.ReflectTest2.main(ReflectTest2.java:17)
- package advstudy;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.util.Collection;
- import java.util.Properties;
- public class ReflectTest2 {
- public static void main(String[] args)throws Exception{
-
- InputStream ips = new FileInputStream("config.properties");
- //InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("advstudy/config.properties");//(问题在这,如果注释掉上面第11行,用这一行的话就会报错,请问是不是文件路径不对,应该是什么路径。adcstudy是eclipse工程对应的src下的文件夹)
-
- Properties props = new Properties();
- props.load(ips);
- ips.close();
- String className = props.getProperty("className");
- Collection 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());
- }
- }
复制代码 |
|