public static Action createAction(String className) {
try {
return (Action) loadClass(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static Class loadClass(String className) {
Class clazz = null;
try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(
className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (clazz == null) {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return clazz;
}为什么要if判断一下???也就是什么情况下clazz是空的呢?

解决方案 »

  1.   

    根据你给的这个className木有找到类的时候就空了
      

  2.   

    难道用Class.forName(className);就不空了吗?都不空为什么还要2次验证呢?
      

  3.   

    Class.forName(String className)返回指定类名的class对象
    如:
    reflect1 = Class.forName("com.freeplatform.zichan.reflect.Reflect_00");
    System.out.println("类名称:" + reflect1.getName());如果你指定的这个类名路径找不到对应的类,那就是null;明白了吗???再想想!!!
      

  4.   

    也可能空,先Thread.currentThread().getContextClassLoader().loadClass(
    className);用当前线程的ClassLoader去装载这个className对应的类,在这里得到不空之后就不进下一个Class.forName(className);了嘛!