java.lang.InstantiationException 实例化异常。当试图通过newInstance()方法创建某个类的实例,而该类是一个抽象类或接口时,抛出该异常。
Fragment$InstantiationException的原因分析 在编写Fragment类的代码时候,Android Lint有时会提示如下error: Avoid not-default constructors Fragment.class.isAssignableFrom(clazz)) { throw new InstantiationException("Trying e) { throw new InstantiationException("Unable to instantiate fragment " + fname constructor that is public", e); } catch (IllegalAccessException e) { throw new InstantiationException 因为非静态内部类对象的实例化需要先实例化外部类对象,仅仅实例化非静态内部类必然抛出InstantiationException。
技术博客文章:解析Java中的java.lang.InstantiationException异常引言在Java开发过程中,遇到异常是家常便饭,但理解并解决这些异常对于提高开发效率和代码质量至关重要。 java.lang.InstantiationException是Java中的一个常见异常,特别是在使用反射(Reflection)和类加载(Class Loading)时。 什么是java.lang.InstantiationException? 触发场景抽象类或接口实例化:尝试实例化一个抽象类或接口时,会抛出InstantiationException。 JPA中的InstantiationException在JPA中,InstantiationException可能不那么直接地出现,但通常与实体类的定义和配置有关。
default constructor for entity: : com.easy.kotlin.entity.People; nested exception is org.hibernate.InstantiationException spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] ... 6 common frames omitted Caused by: org.hibernate.InstantiationException
程序开发中遇到了android.support.v4.app.Fragment$InstantiationException这个bug,给出的提示是: make sure class name exists
/ 张三 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException / 18 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException / 20 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException / 张三 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException / 18 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException > clazz, Context context) throws InstantiationException, IllegalAccessException, public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException > clazz, Context context) throws InstantiationException, IllegalAccessException,
也就是为了创建实例, 我们下面再一起回顾newInstance()方法 ---- newInstance()方法,深入 我们先看一下源码 public T newInstance() throws InstantiationException cachedConstructor = c; } catch (NoSuchMethodException e) { throw new InstantiationException )null); * 具体的结果,我们可以看见,和前面DoClass类的newInstance方法结果一样 * @throws IllegalAccessException * @throws InstantiationException InvocationTargetException */ private void doConstructorNewInstance1() throws IllegalAccessException, InstantiationException */ private void doConstructorNewInstance2R1() throws IllegalAccessException, InstantiationException
; } } public class TestDemo { public static void main(String [] args) throws InstantiationException {// 工厂类 @SuppressWarnings("deprecation") public static Fruit getInstance(String className) throws InstantiationException e) { } return f ; } } public class TestFactory { public static void main(String[] args) throws InstantiationException java.lang.reflect.Method; public class TestDemo { public static void main(String [] args) throws InstantiationException java.lang.reflect.Field;; public class TestDemo { public static void main(String [] args) throws InstantiationException
x.printStackTrace(); } } } 运行如下: $ java RestoreAliases Mail keys: Duke Fang 问题排查 由于没有无参数构造器抛出:InstantiationException > c = Class.forName("ConstructorTrouble"); Object o = c.newInstance(); // InstantiationException more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (InstantiationException more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (InstantiationException x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } catch (InstantiationException
** * 通过类反射 */ @Test public void testClassReflect() throws IllegalAccessException, InstantiationException NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException testConstructReflect() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException testConstructWithParamReflect() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
@throws NoSuchFieldException * @throws ParseException */ public List findAll() throws InstantiationException ; } /** * 执行查询操作,获取查询结果集的第一个 * * @return 查询结果集的第一个,null则查询不到 * @throws InstantiationException @throws NoSuchFieldException * @throws ParseException */ public T findFirst() throws InstantiationException * * @param clazz class * @return 字段列表 * @throws NoSuchFieldException * @throws InstantiationException * @param idValue idValue * @return object * @throws NoSuchFieldException * @throws InstantiationException
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException (); } private static void method4() throws ClassNotFoundException, NoSuchMethodException, InstantiationException System.out.println(student); } private static void method3() throws ClassNotFoundException, InstantiationException student); } private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException
main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { test(); } public static void test() throws ClassNotFoundException, InstantiationException
IllegalAccessException 处理构造器调用异常 构造器调用主要是用于对象的实例化,先看newInstance方法的签名: public T newInstance(Object ... initargs) throws InstantiationException , IllegalAccessException, IllegalArgumentException, InvocationTargetException InstantiationException: 这里只举个例子说明一下InstantiationException出现的场景: public abstract class AbstractSample { public AbstractSample declaredConstructor.newInstance(); } } 像上面的抽象类AbstractSample包含一个默认的公有构造,使用Constructor#newInstance()会抛出InstantiationException 异常: Exception in thread "main" java.lang.InstantiationException at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance
targetObj = null; try { targetObj = targetClass.newInstance(); } catch (InstantiationException e) { log.error("sourceObj:{},targetClass:{},InstantiationException errMsg:{}", sourceObj
22日 */ public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException 22日 */ public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException 22日 */ public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException 22日 */ public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException 22日 */ public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException
12 * 13 * @param args 14 * @throws ClassNotFoundException 15 * @throws InstantiationException * @throws ClassNotFoundException 104 * @throws IllegalAccessException 105 * @throws InstantiationException 106 */ 107 public static void Demo3() throws ClassNotFoundException, InstantiationException */ 131 public static void Demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException ) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, InstantiationException
main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException