Logic.class.getResource (& ldquo; effects \\ newball.wav & rdquo;); returns null

advertisements

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


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

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