0

I wrote this while following a video tutorial. The tutorial showed the same thing but got no error!

Here's the code:

public static void main(String[] args){
    
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = bf.newDocumentBuilder();

        //says input stream cannot be null
        Document d = db.parse(XmlReader.class.getResourceAsStream("XMLFile.xml")); 

        d.normalize();

When I run the program, it says that the input stream cannot be null for the statement indicated statement but, its not null!. The file path is correct! Is there something I'm missing? Is there a problem with eclipse or XMLfiles or something?

Stack trace:

Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at io.XMLFileReading.main(XMLFileReading.java:27)
3
  • 1
    Post the full stacktrace Commented Jul 11, 2015 at 14:33
  • Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at io.XMLFileReading.main(XMLFileReading.java:27) Commented Jul 11, 2015 at 14:38
  • this may help: stackoverflow.com/questions/23997031/… Commented Jul 11, 2015 at 14:40

1 Answer 1

3

Seems like using FileInputStream would do the job (rather than XmlReader.class.getResourceAsStream()).

Document document = dBuilder.parse(new FileInputStream("XMLFile.xml"));

See similar question and accepted answer here: Cannot work with XML file. InputStream is null

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.