I'm writing an application where I'm using XPath to parse an xml file. Before i call DocumentBuilder's parse method, I create a File from the filename, and check if the file exists. This check returns true, but when i try to parse the file I get a nullpointerexception with this stacktrace:
Exception in thread "main" java.lang.NullPointerException
at java.io.WinNTFileSystem.normalize(Unknown Source)
at java.io.WinNTFileSystem.getUserPath(Unknown Source)
at java.io.WinNTFileSystem.resolve(Unknown Source)
at java.io.File.getAbsolutePath(Unknown Source)
at java.io.File.getAbsoluteFile(Unknown Source)
at java.io.File.toURI(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at FileHandler.parseFile(OriginatorHandler.java:112)
at Main.main(Main.java:58)
The file is in the same folder as I run the .jar application, and it has the right content. Here is my code:
public Map<String, List<String>> parseFile(String fileName) {
File file = new File(fileName);
if (!file.exists()) {
System.out.println("Couldn't find file [" + fileName + "]");
System.exit(-1);
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
try {
builder = factory.newDocumentBuilder();
//This is where the exception is thrown
document = builder.parse(file);
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
return getCaGroups(document, xPath);
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
What can this error come frome? The file exists, it isn't null and is in the folder where I run the .jar file. What else could cause this problem?
Please put the Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
100 % you are successfully in you job.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.