我有生成KML文件的代码,除了我创建实际文件的部分外,一切都很好。我从互联网上读过一些例子,但我在所有这些例子中都遇到了同样的错误,这是我的代码:
public void crearMapa() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
kml = docBuilder.newDocument();
Element document = kml.createElement("Document");
kml.appendChild(document);
definirConnex(document);
for(int i = 0; i < a_etnies.size(); i++)
definirTot(i,document);
for (Entry<String, Enclavament> entry : a_enclavaments.entrySet())
colocacio(entry.getValue(),document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(kml);
StreamResult result = new StreamResult(new File("mapa.kml"));
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
我尝试了另一个代码,但我得到了同样的错误:
TRANSFORMER EXCEPTION!javax.xml.transform.TransformerException: java.lang.NullPointerException
在这一行:
transformer.transform(source, result);
我不知道为什么会发生这种情况。还有其他方法吗?
编辑:代码更新为更干净的版本。 kml是文档类型