[英]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. 我有一个Maven项目,我正在运行测试,他们不会成功完成。 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).
问题是测试在尝试加载文件时返回NullPointerException(我认为这是由于错误的文件路径,但由于某种原因我无法正确)。
Here is the location of my test.java file: 这是我的test.java文件的位置:
C:_MY_PROJECTS\\P1\\trunk\\P1\\modules\\P1-commons\\P1-core\\src\\test\\java\\si\\smth\\P1\\test C:_MY_PROJECTS \\ P1 \\树干\\ P1 \\模块\\ P1-公地\\ P1-芯\\ SRC \\测试\\ java中\\ SI \\不便\\ P1 \\测试
This is where I keep the resources: 这是我保留资源的地方:
C:_MY_PROJECTS\\P1\\trunk\\P1\\modules\\P1-commons\\P1-core\\src\\test\\resources C:_MY_PROJECTS \\ P1 \\树干\\ P1 \\模块\\ P1-公地\\ P1-芯\\ SRC \\测试\\资源
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...
它曾经在Ant上工作 - 现在我已经为Maven重新构建了项目,我似乎无法让它工作......
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? 在测试运行时,它返回一个NullPointerException,我假设文件路径出错? Any insight on this matter?
有关这件事的任何见解?
Thanks in advance, Bg 在此先感谢,Bg
I have the document on C:\\Users\\MyProfile\\Documents\\MyProject\\Repo\\java-platform\\src\\main\\resources\\reports 我在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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.