0

When I try to play a sound I get a an error:

java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(Unknown Source)
    at java.applet.Applet.newAudioClip(Unknown Source)
    at com.artem.megatale.Sound.<init>(Sound.java:13)
    at com.artem.megatale.Sound.<clinit>(Sound.java:7)
    at com.artem.megatale.Level.building(Level.java:340)
    at com.artem.megatale.Level.tick(Level.java:371)
    at com.artem.megatale.Component.tick(Component.java:111)
    at com.artem.megatale.Component.run(Component.java:212)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-4" java.lang.NullPointerException
    at com.artem.megatale.Sound$1.run(Sound.java:23)
Exception in thread "Thread-5" java.lang.NullPointerException
    at com.artem.megatale.Sound$1.run(Sound.java:23)

My sound Class:

public class Sound {
    public static final Sound blockBreak = new Sound("/sounds/break_stone.wav");

    private AudioClip clip;

    public Sound(String name) {
        try {
            clip = Applet.newAudioClip(Sound.class.getResource(name));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void play() {
        try {
            new Thread() {
                public void run() {
                    clip.play();
                }
            }.start();
            Thread.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I play the sound using Sound.blockBreak.play(); The sound is stored in res/sound/file, and it is stored in the jar file. I tried changing the blockBreak = new Sound("/sounds/break_stone.wav"); to blockBreak = new Sound("res/sounds/break_stone.wav"); but I still get the error.

Someone asked for a screenshot of my packages:

So here is the screenshot

Someone also said that I should get rid of the try and catch so did that and got 1 other error:

Exception in thread "Thread-3" java.lang.ExceptionInInitializerError
    at com.artem.megatale.Level.building(Level.java:340)
    at com.artem.megatale.Level.tick(Level.java:371)
    at com.artem.megatale.Component.tick(Component.java:111)
    at com.artem.megatale.Component.run(Component.java:212)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(Unknown Source)
    at java.applet.Applet.newAudioClip(Unknown Source)
    at com.artem.megatale.Sound.<init>(Sound.java:13)
    at com.artem.megatale.Sound.<clinit>(Sound.java:7)
    ... 5 more

The com.artem.megatale.Level.building(Level.java:340) is

Sound.blockBreak.play();
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – davidxxx Apr 19 '17 at 07:10
  • I'm assuming the `res/sound` is stored within the Jar and it's `sound` or `sounds`? – MadProgrammer Apr 19 '17 at 07:13
  • It could be helpful if you upload the error log, or make us sure that `NullPointerException` is being thrown at the `Sound` variable declaration. Also a scheme or a screenshot of your packages structure could help to resolve your problem. – Joe Hackerberg Apr 19 '17 at 07:33
  • **Do not be overdefensive**, remove the unnecessary try catches and let the actual exception occur directly (though you are printing the stack trace, but still it seams somehow you are missing them). Just let the original root cause code fire the exception. Then you will get to know the exact cause and you would be able to modify and correct the actual cause of the exception – nits.kk Apr 19 '17 at 08:05
  • I did what you all said and updated the question –  Apr 19 '17 at 19:47
  • Can you unzip the jar and confirm that the "res" folder is in there? My guess is that it's not being packaged up correctly. – Dan W Apr 19 '17 at 19:49
  • @DanW I exported it a runnable jar file, ran it and the sound plays, but it doesn't when I try to run it in eclipse. –  Apr 19 '17 at 20:03
  • @Artem You'll need to add that directory to your Eclipse classpath. See - http://stackoverflow.com/a/25163361/864369 – Dan W Apr 19 '17 at 20:48
  • @DanW I still get a null pointer exception –  Apr 20 '17 at 02:06
  • I still need a solution –  Jun 11 '17 at 23:44

0 Answers0