gpt4 book ai didi

java - 变换返回空指针异常但参数不为空?

转载 作者:行者123 更新时间:2023-12-01 06:16:19 29 4
gpt4 key购买 nike

我正在生成一个 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)

最佳答案

尝试下面的代码

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

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

关于java - 变换返回空指针异常但参数不为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23744064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com