Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

java - getResourceAsStream returning null

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: srccommyProjectClassIMakeTheCallFrom.java srccommyProjectMyImage.png

also a similar structure exists in: buildclassescommyProjectClassIMakeTheCallFrom.class buildclassescommyProjectMyImage.png

However when I make the call:

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

It comes out null. Am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...