i made a simple game in java and i want to add a song in the background, i checked everywhere, looked in guids and nothing worked, can some one please tell how to do it? this is the closest i got :
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("res/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace;
System.err.println(e.getMessage());
}
}
}).start();
}
this gives me a noll pointer exception, can any one tell me how to fix this code or write a new one that will work? the stack trace is : java.lang.NullPointerException at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:130) at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113) at Frame$1.run(Frame.java:59) at java.lang.Thread.run(Thread.java:745)
(line 59 is : AudioInputStream inputStream = AudioSystem.getAudioInputStream(Frame.class.getResourceAsStream("res/" + url));