转换返回空指针异常,但参数是否为null?

时间:2014-05-19 17:48:15

标签: java nullpointerexception kml transformer

我正在生成一个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.out,但它也不打印最后一个。为什么给我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)

1 个答案:

答案 0 :(得分:-1)

尝试以下代码

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

它可能返回null,使你的变换器对象为空。