getResourceAsStream returning null

江枫思渺然 提交于 2019-11-27 07:26:58

问题


I've seen many other people ask this question here and here, but it seems that I have done everything necessary yet I still get null when using the getResourceAsStream() call.

In netbeans I dragged my image into my package and it shows up in the folder in which the source code and .class files are held. So I have: src\com\myProject\ClassIMakeTheCallFrom.java src\com\myProject\MyImage.png

also a similar structure exists in: build\classes\com\myProject\ClassIMakeTheCallFrom.class build\classes\com\myProject\MyImage.png

However when I make the call:

System.out.println(dolphinWater.class.getClassLoader().getResourceAsStream("myImage.png"));

It comes out null. Am I missing something?


回答1:


Either put the file directly in the src folder, not the package folder. Or use getResourceAsStream("com/myProject/myImage.png").

http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)



来源:https://stackoverflow.com/questions/7017361/getresourceasstream-returning-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!