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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 严学韦 于 2012-12-21 14:01 编辑

classname = java.util.ArrayList
---------------------------------------------------------------------
package packge1;

public class ReflectPoint {
        @Override
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + x;
                result = prime * result + y;
                return result;
        }

        @Override
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                ReflectPoint other = (ReflectPoint) obj;
                if (x != other.x)
                        return false;
                if (y != other.y)
                        return false;
                return true;
        }

        private int x;
        public int y;
        public String str1 = "ball";
        public String str2 = "base";
        public String str3 = "bpple";
        
        public ReflectPoint(int x, int y) {
                super();
                this.x = x;
                this.y = y;
        }
        
        public String toString(){
                return str1 + ":"+str2 +":"+ str3;
        }

}
------------------------------------------------------------
package packge1;

import java.io.*;
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");
                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);
               
                System.out.println(collections.size());
        
        }
}

运行时报异常了,为什么呀,不应该呀

异常.jpg (20.16 KB, 下载次数: 38)

异常.jpg

3 个回复

倒序浏览
从错误代码上看应该是空指针异常
回复 使用道具 举报
好像找不到配置文件,可是配置文件在那边呢呀
回复 使用道具 举报
李东城 来自手机 中级黑马 2013-2-4 12:07:46
板凳
自定义类要提供空参数的构造方法,newInstance()就是利用反射调用空参数的构造方法!来自: Android客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马