简体   繁体   中英

this.getClass().getResource().openStream(“file.xls”) returns NullPointerException (MAVEN project)

I have a Maven project that I am running tests on, and they wont successfully complete. The issue is that the test returns a NullPointerException when attempting to load a file (which I believe is due to a wrong filepath, but I cannot get it right for some reason).

Here is the location of my test.java file:

C:_MY_PROJECTS\\P1\\trunk\\P1\\modules\\P1-commons\\P1-core\\src\\test\\java\\si\\smth\\P1\\test

This is where I keep the resources:

C:_MY_PROJECTS\\P1\\trunk\\P1\\modules\\P1-commons\\P1-core\\src\\test\\resources

Am I missing something here? It used to work on Ant - now that I've re-structured the project for Maven I cannot seem to get it working...

The code:

@Test
  public void massImportGDTest_2() throws IOException {
    InputStream in = this.getClass().getResource("resources/gd_mass.xls").openStream();
    CAopParserMass parser = new CAopParserMass(in);
    CInfo[] result = parser.parse();
    assertEquals(11, result.length);        
  }   

As the test runs it returns a NullPointerException, I assume cause of a faulty filepath? Any insight on this matter?

Thanks in advance, Bg

I have the document on C:\\Users\\MyProfile\\Documents\\MyProject\\Repo\\java-platform\\src\\main\\resources\\reports

And this run for me:

java.io.InputStream in = this.getClass().getResource("/reports/gd_mass.xls").openStream();

Change "/" for "\\" and it may not be necessary to indicate "resources" because it starts from there. Try this:

InputStream in = this.getClass().getResource("/gd_mass.xls").openStream();

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