本帖最后由 tacyjay在路上 于 2014-5-20 08:52 编辑
张孝祥老师第28个视频中,按老师的步骤敲得,出现空指针异常,找不出原因,求大神。
问题见代码。
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Properties;
- public class ReflectTest2 {
- public static void main(String[] args)throws Exception {
-
- InputStream ips = ReflectTest2.class.getClass().getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");
- //这里为什么报NullPointerException???
-
- Properties props = new Properties();
- props.load(ips);
- ips.close();
- String className = props.getProperty("className");
- Collection collections =(Collection) Class.forName(className).newInstance();
-
- ReflectPoint pt1 = new ReflectPoint(3,3);
- ReflectPoint pt2 = new ReflectPoint(5,5);
- ReflectPoint pt3 = new ReflectPoint(3,3);
- collections.add(pt1);
- collections.add(pt2);
- collections.add(pt3);
- collections.add(pt1);
- System.out.println(collections.size());
- }
- }
- class ReflectPoint {
- private int x;
- public int y;
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public ReflectPoint(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
- }
复制代码 |