0

I am working on a program to process an XML file. The resources file contains an exercise.xml file. The following code should read the file from the resources stream:

private static InputStream readXmlFileIntoInputStream(final String fileName) {
        return Main.class.getClassLoader().getResourceAsStream(fileName);
    }
    
    public static Document readXML(String fileName) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // This is used to prevent xml injection attacks        
        try(InputStream inputStream = readXmlFileIntoInputStream("exercise.xml")){
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(inputStream);
            doc.getDocumentElement().normalize();
        }
        catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
            return doc;
    }

However the actual result is:

Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:117)
    at main.Main.main(Main.java:87)

The expected result should be a processed XML file.

halfer
  • 19,824
  • 17
  • 99
  • 186
Evan Gertis
  • 1,796
  • 2
  • 25
  • 59

0 Answers0