1

我正在生成一个 KML 文件,在我使用转换器 API 的地方,我得到了 NullPointerException 异常。这也是我引用的代码:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(kml);
            StreamResult result = new StreamResult(new File("src/mapa/mapa.kml"));
            System.out.print("DESPRES DE CREAR RESULT\n");
            if(source == null)
                System.out.print("SOURCE IS NULL");
            if(result == null)
                System.out.print("RESULT IS NULL");
            transformer.transform(source, result);
            System.out.print("AFTER TRASNFORM\n");

它不打印任何 NULL System.outs,但也不打印最后一个。为什么它给我NPE?

例外:

DESPRES DE CREAR RESULT
ERROR:  ''
javax.xml.transform.TransformerException: java.lang.NullPointerException
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at Joc.crearMapa(Joc.java:199)
    at MastersOfWar.main(MastersOfWar.java:18)
Caused by: java.lang.NullPointerException
    at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
    ... 4 more
---------
java.lang.NullPointerException
    at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at Joc.crearMapa(Joc.java:199)
    at MastersOfWar.main(MastersOfWar.java:18)
4

1 回答 1

-1

试试下面的代码

System.out.print(transformerFactory.newTransformer());

它可能返回 null,使您的转换器对象为 null。

于 2014-05-19T17:52:44.950 回答