简体   繁体   中英

IntelliJ IDEA return null with ClassLoader.getSystemResourceAsStream(“myfile.xml”));

I've got this code (link)

The DOM parser example works fine on codeboard, but I have one error on IntelleJ IDEA that I can't figure out.

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

The next line returns NULL :

Document document = builder.parse(ClassLoader.getSystemResourceAsStream("Student.xml"));

I have made it work changing ...builder.parse(ClassLoader.getSystemResourceAsStream... to ...builder.parse(new FileInputStream... and the full path of Student.xml :

Document document = builder.parse(new FileInputStream("G:\JAVA\edxGalileo\DOMParser\src\com\pedro\Student.xml"));

What's wrong with Intellij that it does not work?

You need to make sure that Student.xml is located in a folder that is marked as a resources root in the IntelliJ IDEA project structure. Then it will be copied to the output directory together with the .class files, and you'll be able to access it using getResourceAsStream() .

在此处输入图片说明

  1. you can create a project and
  2. define a source root
  3. put the xml into resources folder

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM