简体   繁体   English

this.getClass()。getResource()。openStream(“file.xls”)返回NullPointerException(MAVEN项目)

[英]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.

相关问题 this.getClass()。getResource(“”)。getPath()返回错误的路径 - this.getClass().getResource(“”).getPath() returns an incorrect path 当使用IntelliJ / Jetty 8.1.14运行项目时,this.getClass()。getResource(“”)。getFile()返回的路径开头为“ file:”。 - this.getClass().getResource(“”).getFile() returning path with “file:” at the beginning when running project with IntelliJ/Jetty 8.1.14 this.getClass()。getClassLoader()。getResource()==异常 - this.getClass().getClassLoader().getResource() == Exception URL 资源 = this.getClass().getResource(“文件名”) 是 null - URL resource = this.getClass().getResource(“file-name”) is null Java getClass().getResource("file") 导致 NullPointerException - Java getClass().getResource("file") leads to NullPointerException 如何使用this.getClass()。getResource(String)? - How to use this.getClass().getResource(String)? Openshift this.getClass()。getResource()路径可能不正确 - Openshift this.getClass().getResource() path probably not correct 使用this.getClass()。getDeclaredField方法时发生NullPointerException - NullPointerException while using this.getClass().getDeclaredField method 等价于Jar中的this.getClass()。getClassLoader()。getResource(“。”)。toURI() - Equivalent of this.getClass().getClassLoader().getResource(“.”).toURI() from within a Jar 通过 getClass().getResource() 加载文件 - File loading by getClass().getResource()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM