请大家帮忙看下,这段代码怎么会出现NullPointerException呢?想不通啊~- package cn.itcast.day3;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.Collection;
- public class ActiveProxy {
- public static void main(String[] args) throws Exception{
- Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
- System.out.println(clazzProxy.getName());
- System.out.println("----------begin constructors list----------");
- Constructor[] constructors = clazzProxy.getConstructors();
- for(Constructor constructor : constructors){
- String name = constructor.getName();
- StringBuilder sBuilder = new StringBuilder(name);
- sBuilder.append('(');
- Class[] clazzParams = constructor.getParameterTypes();
- for(Class clazzParam : clazzParams){
- sBuilder.append(clazzParam.getName()).append(',');
- }
- if(clazzParams!=null && clazzParams.length != 0)
- sBuilder.deleteCharAt(sBuilder.length()-1);
- sBuilder.append(')');
- System.out.println(sBuilder.toString());
- }
- System.out.println("----------begin methods list----------");
- Method[] methods = clazzProxy.getMethods();
- for(Method method : methods){
- String name = method.getName();
- StringBuilder sBuilder = new StringBuilder(name);
- sBuilder.append('(');
- Class[] clazzParams = method.getParameterTypes();
- for(Class clazzParam : clazzParams){
- sBuilder.append(clazzParam.getName()).append(',');
- }
- if(clazzParams!=null && clazzParams.length != 0)
- sBuilder.deleteCharAt(sBuilder.length()-1);
- sBuilder.append(')');
- System.out.println(sBuilder.toString());
- }
- System.out.println("----------begin create instance object----------");
- Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class);
- Collection proxy = (Collection)constructor.newInstance(new InvocationHandler(){
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- return null;
- }
- });
- System.out.println(proxy);
- proxy.clear();
- }
- }
复制代码 运行的结果最后显示NullPointerException异常,这是为什么呢?请大家指教,谢谢!
|