为什么会报错?能不能说说晕倒这种错误时,通常要怎么做?先谢谢~~
package chengengsheng.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;
public class ProxyTest implements InvocationHandler
{
private Object obj;
public ProxyTest(Object object)
{
this.obj = object;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
System.out.println("Star");
method.invoke(obj, args);
System.out.println("Over");
return null;
}
public static void main(String[] args)
{
List v = new Vector();
InvocationHandler p = new ProxyTest(v);
Class<?> classty = p.getClass();
List l = (List) Proxy.newProxyInstance(classty.getClassLoader(),
v.getClass().getInterfaces(), p);
l.add("mipaifu"); //第35行
}
}
报错:
Star
Over
Exception in thread "main" java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.Boolean
at $Proxy0.add(Unknown Source)
at chengengsheng.proxy.ProxyTest.main(ProxyTest.java:35)
------解决方案--------------------------------------------------------
- Java code
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.List;import java.util.Vector;public class ProxyTest implements InvocationHandler{ private Object obj; public ProxyTest(Object object) { this.obj = object; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Start"); Object o=method.invoke(obj, args); System.out.println("Over"); return o; } public static void main(String[] args) { List v = new Vector(); InvocationHandler p = new ProxyTest(v); Class<?> classty = p.getClass(); List l = (List) Proxy.newProxyInstance(classty.getClassLoader(), v.getClass().getInterfaces(), p); l.add("mipaifu"); //第35行 for(Object o:l){ System.out.println(""+o); } }}
------解决方案--------------------------------------------------------
遇到问题首先看类似这样的地方Exception in thread "main" java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.Boolean,这儿(java.lang.ClassCastException)就说了是类转换异常,然后再看java.util.Vector cannot be cast to java.lang.Boolean这个说vector不能转换成Boolean型。一般的错误下面还会有cause by:.......什么的,看cause by 也可以。java的错误、异常一般还会说具体是程序或者配置哪儿出了错,这个需要你慢慢去体会了,但是一般都比较明显。