SAXUnmarshaller java.lang.NullPointerException

时间:2014-10-20 14:30:23

标签: java jaxb unmarshalling

在解组xml内容时,我得到以下异常。我在weblogic 12c下运行并拥有xercerimpl.jar& xalan.jar罐装在战争中。请告知此错误可能存在的问题。

Exception Description: An error occurred while trying to instantiate the schema platform.
Internal Exception: java.lang.NullPointerException
    at org.eclipse.persistence.exceptions.XMLMarshalException.errorInstantiatingSchemaPlatform(XMLMarshalException.java:171)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getXMLReader(SAXUnmarshaller.java:173)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:333)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:320)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:619)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:408)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:149)
    ... 73 more
Caused by: java.lang.NullPointerException
    at org.apache.xerces.parsers.AbstractSAXParser.setEntityResolver(AbstractSAXParser.java:1177)
    at org.eclipse.persistence.internal.oxm.record.XMLReader.setEntityResolver(XMLReader.java:86)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getXMLReader(SAXUnmarshaller.java:166

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法作为解决方法,以便在正确的位置获得EntityResolver

import java.io.IOException;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBHelper;
import org.eclipse.persistence.jaxb.JAXBUnmarshaller;
import org.eclipse.persistence.oxm.XMLUnmarshaller;
import org.xml.sax.EntityResolver;
import org.xml.sax.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Demo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBUnmarshaller jaxbUnmarshaller = JAXBHelper.getUnmarshaller(unmarshaller);
        XMLUnmarshaller xmlUnmarshaller = jaxbUnmarshaller.getXMLUnmarshaller();
        xmlUnmarshaller.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return null;
            }

        });
    }

}

  

我在weblogic 12c下运行并拥有xercerimpl.jar& xalan.jar   战争中包装的罐子。

为什么你的战争中包装了xerceimpl.jar和xalan.jar?

  

这些罐子被组件添加到Maven的战争项目中。

我建议您在Maven设置中修复此问题。


更新

  

解组逻辑是用单独的组件编写的   无法改变它(适用于其他项目)。有没有我可以解决它   在我身边(在网络项目内)?这是组件代码,   JAXBContext jc = JAXBContext.newInstance(Config.class);解组   unmarshaller = jc.createUnmarshaller();结果结果=(配置)   的Unmarshaller.unmarshal(URL);

尝试以下操作会导致MOXy沿着利用StAX解析器而不是SAX解析器的路径前进:

InputStream inputStream = url.openStream;
Result result = (Config) unmarshaller.unmarshal(inputStream);
inputStream.close();