1

I am working on a Javafx game and have been attempting to implement some sound using the following code:

URL laserResource = getClass().getResource("LaserBlast.wav");
AudioClip laserPlayer = new AudioClip(laserResource.toString());

This code is instantiated in the main class before any methods. I have a module called JavaFxImages which contains all my resources including my sounds and I have been successfully able to access images for ImageView from the folder through code such as Image asteroid = new Image("Asteroid.png");. However, when I run my game I get a NullPointerException at the line for my audio clip. I have spent the past many hours researching and trying every different way to access this resource. I've tried many permutations of the URL ("JavaFxImages/LaserBlast.wav", Desktop/JavaFxImages/LaserBlast.wav", etc.), but every time it has the same error. I'm really at a loss for what is wrong with the code. I would greatly appreciate any help that can be given. Thanks in advance!

The full error message is given below.

Error Message:
Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct         Application instance: class sample.Main
    at   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NullPointerException
    at sample.Main.<init>(Main.java:76)
    ... 10 more
Phil
  • 19
  • 3
  • Can you show us the class implementation? The first few lines. As the error mentioned "Unable to construct Application instance: class sample.Main", that is the constructor error. In other words, your class constructor may have something wrong. – PSo May 04 '16 at 02:09
  • 2
    The path to the .wav file is wrong. – James_D May 04 '16 at 02:26
  • Let's assume when you run your application you package it as a jar. If so run "jar tvf .jar" where you replace with your jar file. Check that LaserBlast.wav is in whatever jar location that the class you are calling `getClass().getResource("LaserBlast.wav");` is in (I assume that would be `/sample/Main/'). If it is not there you have a packaging issue where your build is not copying the wav file across. If it is there but in a different location, then you have it in the wrong spot. If it is not in a jar, you should find it on your file system next to sample/Main.class – jewelsea May 04 '16 at 03:02
  • Thank you to all who answered, you have solved the problem for me. I created a new directory called JavaFxSounds and put it in /src/sample instead of simply inside my project. I am still a little confused as to how I can access image resources from a directory outside of my main class but not sound files, but at least the sound is working! Thanks again. – Phil May 04 '16 at 13:41

1 Answers1

-1

Your error mostly happens on Itellij IDEA - Javafx Project

Instead of:

URL laserResource = getClass().getResource("LaserBlast.wav");
AudioClip laserPlayer = new AudioClip(laserResource.toString());

Use:

//By default your Package_NAME is: sample
File laserResource = new File("src/Package_NAME/LaserBlast.wav");
AudioClip laserPlayer = new AudioClip(laserResource.toURI().toString());
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Ralph Alex
  • 89
  • 1
  • 4
  • Most certainly not: resource lookup should happen from the package path, not the file path (and absolutely _not_ a file path related to the source which is not available at runtime – kleopatra Oct 07 '20 at 22:05