Java, java.lang.reflect.InvocationTargetException

2019-07-21 09:57发布

问题:

I've a Java app, working with Lucene and Apache commons Digester to parse a xml collection. After the first item is parsed, I get this error... What does it mean ?

Feb 24, 2011 5:53:16 PM org.apache.commons.digester.Digester endElement
SEVERE: End event threw exception
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:216)
    at org.apache.commons.digester.SetNextRule.end(SetNextRule.java:220)
    at org.apache.commons.digester.Rule.end(Rule.java:257)
    at org.apache.commons.digester.Digester.endElement(Digester.java:1345)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
    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:2938)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.commons.digester.Digester.parse(Digester.java:1871)
    at CollectionIndexer.main(CollectionIndexer.java:103)
Caused by: java.lang.NullPointerException
    at CollectionIndexer.addDocument(CollectionIndexer.java:41)
    ... 20 more
Exception in thread "main" java.lang.NullPointerException
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3363)
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3389)
    at org.apache.commons.digester.Digester.endElement(Digester.java:1348)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
    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:2938)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.commons.digester.Digester.parse(Digester.java:1871)
    at CollectionIndexer.main(CollectionIndexer.java:103)
Caused by: java.lang.NullPointerException
    at CollectionIndexer.addDocument(CollectionIndexer.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:216)
    at org.apache.commons.digester.SetNextRule.end(SetNextRule.java:220)
    at org.apache.commons.digester.Rule.end(Rule.java:257)
    at org.apache.commons.digester.Digester.endElement(Digester.java:1345)
    ... 12 more

回答1:

Looks pretty clear to me:

Caused by: java.lang.NullPointerException
    at CollectionIndexer.addDocument(CollectionIndexer.java:41)

Your problem is right there. You need to more defensive in your code to protect against null references.

P.S. It's very bad practice to put your code into the default package. You should always put classes into an explicit package.



回答2:

Is CollectionIndexer your own class? It seems like it throws NullPointerException on line 41. It is invoked by SAX event parser, hence being wrapped by InvocationTargetException.

BTW: JVM prints stack traces starting from the outermost one, displaying the innermost exception at the end. But since the innermost exception is thrown at the beginning, it is almost always the most interesting one. So as a rule of thumb: always look at the last caused by clause in exception stack trace, as it is typically the most interesting.