Java教程

关于实例化异常java.lang.InstantiationException

本文主要是介绍关于实例化异常java.lang.InstantiationException,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

遇到的问题,报错信息如下:

项目场景:

在项目开发过程中遇到对象实例化报错的问题,查看其原因和解决方法,记录在博客中。


问题描述:

遇到的报错信息如下图所示,对象实例化错误
在这里插入图片描述

java.lang.InstantiationException
	at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at com.raycloud.dmj.erp.sandbox.controller.platform.fxg.FxgSdkController.buildErrorResponse(FxgSdkController.java:151)
	at com.raycloud.dmj.erp.sandbox.controller.platform.fxg.FxgSdkController.getOrdersDerail(FxgSdkController.java:105)
	at com.raycloud.dmj.erp.sandbox.controller.platform.fxg.FxgSdkController.getOrderDetail(FxgSdkController.java:87)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)

原因分析:

这个异常多半是由于通过反射在实例化的时候,对应的类里面覆盖了无参构造而导致无法实例化,由于创建类的时候,默认有一个无参构造,前提是没有其他构造方法,如果没有明确的将无参构造写出来,该类就是缺少无参构造了,也就导致了无法实例化。


解决方案:

建议在创建类的时候,建议保留(显式的写出来)无参构造。
在这里插入图片描述
总结原因:通过反射来为某个类创建实例的时候,需要该类存在无参构造,不然就会抛出InstantiationException该异常。

所以最好还是把无参构造保留的好!(ps:如果无参构造的修饰符被修改为其他的protected private default,将会出现另一个异常java.lang.IllegalAccessException)

这篇关于关于实例化异常java.lang.InstantiationException的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!