1

I have project with name 'XMLparser'. In project have one folder with name 'XML' which contains the file 'database.xml' When I try to get directory to database Java show error.

Java error:

Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at XMLparser.main(XMLparser.java:24)

I try to get directory with this code:

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

Project tree:

XMLparser
  src
   - (default package)
     - XMLparser.java (My code is here)

  JRE System Library

  xml
    - database.xml (This is the xml file)

What need to type here ClassLoader.getSystemResourceAsStream("HERE") to work?

GabrielAtan
  • 410
  • 4
  • 11
  • 1
    The xml directory is outside your classpath. Add it or move it under the src directory. – BarrySW19 Oct 30 '14 at 14:04
  • I think you have to create input stream from path. Becase you're trying to call getResource from nonRecource directory. How to get path is explained here: http://stackoverflow.com/questions/16770384/parent-parent-directory-in-java – deathangel908 Oct 30 '14 at 14:05

3 Answers3

1

Try this, if you won't add "/xml/database.xml" in your "src" folder:

final String workingDirectory = System.getProperty("user.dir");
final InputStream in = new FileInputStream(workingDirectory + "/xml/database.xml");
document = builder.parse(in);
..

.

stacky
  • 800
  • 6
  • 18
1

You can not go to the root folder of your application

c4086478
  • 23
  • 4
0

Just remove the first part like this:

document = builder.parse(ClassLoader.getSystemResourceAsStream("xml/database.xml"));

Your xml folder is in the root of your project folder this is where getSystemResourceAsStream looks if I am not mistaken.

brso05
  • 13,142
  • 2
  • 21
  • 40