Mansouritta
Mansouritta

Reputation: 176

Logic.class.getResource("effects\\newball.wav"); returns null

I've got the problem that the following code snip returns null:

System.out.println(Logic.class.getResource("effects\\newball.wav"));

I have a source folder in my project called effects. in this folder there's the referred file. I think there's a syntax error... Because THE FILE IS THERE. I must refer in this way (means with getResource) to my file because I will export it as jar later.

Thank you

Upvotes: 1

Views: 90

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209052

Your effect directory should be a direct child of the src dir. Also in which case, you need a / to start the string path. So you would need this

System.out.println(Logic.class.getResource("/effects/newball.wav"));

ProjectRoot
          src
             effect
                  newball.wav

What I normally do using an IDE is just create a new package and name it whatever I want the file to be - in your case "effect". It's easier that way.


UPDATE

"I did it exatly so, but it still returns null"

It works fine for me

enter image description here

package stackoverflow;

public class Test {

    public static void main(String[] args) {
        System.out.println(Test.class.getResource("/effects/stack_reverse.png"));

    }
}

Output: file:/C:/Android/workspace/StackOverflow/bin/effects/stack_reverse.png

Upvotes: 2

JVMATL
JVMATL

Reputation: 2122

Resource paths should use forward slashes, regardless of the filesystem on the machine you are using: try "effects/newball.wav"

See http://docs.oracle.com/javase/7/docs/technotes/guides/lang/resources.html (Under "resources, Names, and Contexts -- "The name of a resource is independent of the Java implementation; in particular, the path separator is always a slash (/).")

Upvotes: 0

Related Questions