Closing output stream via StreamResult



I am working on an Android application, and so far so good, however I have a small hiccup which is bothering me.


In the project I'm working with XML files, and I'm saving them to the External Storage of the Android Device. To do this I'm using the TransformerFactory and a StreamResult object.



TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer trans = transFactory.newTransformer();
DOMSource xmlSource = new DOMSource(xmlDoc);

StreamResult result = new StreamResult(Environment.getExternalStorageDirectory() + "/file.xml");
trans.transform(xmlSource, result);
result.getOutputStream().close();


So, the saving of the file IS working, but the problem I have, is that on the last line, result.getOutputStream().close(); I am getting a Nullpointer Exception.


So my really simple question: Does the stream need to be closed by me, or do some of the methods close it? When I remove the last line, it still saves the file and gives no errors at all, which I am happy with, but I don't want to leave any streams open.


No comments:

Post a Comment