解决问题:com.alibaba.excel.exception.ExcelAnalysisException: Can not instance class: xx.xx.xx
程序员文章站
2022-03-19 23:38:21
...
com.alibaba.excel.exception.ExcelAnalysisException: Can not instance class: com.atguigu.demo.excel.DemoData
at com.alibaba.excel.read.metadata.holder.AbstractReadHolder.notifyEndOneRow(AbstractReadHolder.java:147)
at com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler.endHandle(ProcessResultCellHandler.java:44)
at com.alibaba.excel.analysis.v07.XlsxRowHandler.endElement(XlsxRowHandler.java:44)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.parseXmlSource(XlsxSaxAnalyser.java:158)
at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.execute(XlsxSaxAnalyser.java:182)
at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:105)
at com.alibaba.excel.ExcelReader.read(ExcelReader.java:180)
at com.alibaba.excel.ExcelReader.read(ExcelReader.java:170)
at com.alibaba.excel.read.builder.ExcelReaderSheetBuilder.doRead(ExcelReaderSheetBuilder.java:160)
at com.atguigu.demo.excel.TestEasyexcel.testread(TestEasyexcel.java:33)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: com.alibaba.excel.exception.ExcelDataConvertException: Can not instance class: com.atguigu.demo.excel.DemoData
at com.alibaba.excel.read.listener.ModelBuildEventListener.buildUserModel(ModelBuildEventListener.java:98)
at com.alibaba.excel.read.listener.ModelBuildEventListener.invoke(ModelBuildEventListener.java:38)
at com.alibaba.excel.read.listener.ModelBuildEventListener.invoke(ModelBuildEventListener.java:28)
at com.alibaba.excel.read.metadata.holder.AbstractReadHolder.notifyEndOneRow(AbstractReadHolder.java:141)
... 41 more
Caused by: java.lang.InstantiationException: com.atguigu.demo.excel.DemoData
at java.lang.Class.newInstance(Class.java:427)
at com.alibaba.excel.read.listener.ModelBuildEventListener.buildUserModel(ModelBuildEventListener.java:94)
... 44 more
Caused by: java.lang.NoSuchMethodException: com.atguigu.demo.excel.DemoData.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 45 more
- 问题描述:在使用easyExcel时,进行读操作,出现如上错误。
- 解决思考:
- 分析:在上述报错信息中,我们关注下面四句输出信息
ExcelAnalysisException: Can not instance class: com.atguigu.demo.excel.DemoData
ExcelDataConvertException: Can not instance class: com.atguigu.demo.excel.DemoData
InstantiationException: com.atguigu.demo.excel.DemoData
NoSuchMethodException: com.atguigu.demo.excel.DemoData.<init>()
- 四句话都有DemoData,不难分析出来该类有问题
- 其次, Can not instance class,不能实例化,可以推出构造方法有问题
- 最后一句:NoSuchMethodException没有此方法,得出缺少无参构造方法
- 定位到该类,发现确实没有无参构造
@Data
@AllArgsConstructor
public class DemoData {
- 解决方法:
添加无参构造:例如
加上该注解@NoArgsConstructor
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DemoData {
或者自己写上无参构造
public DemoData(){}