Java file uri with xml file null pointer exception [closed]
I am using windows OS, and want to parse xml file, but get null pointer error....
public void print( )
throws SAXException, IOException {
DocumentBuilder builder;
开发者_如何学Python Document document = builder.parse( "D:\\my\\xml.xml");//null pointer exception
}
here is the solution:
public class DOMPrinter {
private DocumentBuilder builder;
public void print(String fileName )//, PrintStream out)
throws SAXException, IOException {
try
{
DocumentBuilderFactory fact= DocumentBuilderFactory.newInstance();
builder= fact.newDocumentBuilder();
Document document = builder.parse( fileName);
Node node = document.getDocumentElement();
String root = node.getNodeName();
System.out.println("Root Node: " + root);
}
catch (Exception e) {
// TODO: handle exception
}
}
I didn't use DocumentBuilderFactory to create DocumentBuilder , so that was a error.
You never instantiated builder
. You need to say builder = ...
;
Also, you can't have private fields inside a method. Delete the private keyword.
精彩评论