0

Using getCodeBase() for getAudioClip returns various errors.

I have tried putting my audio file in various locations in my project file* and tried using getDocumentBase instead, but it keeps returning the error.

*I've put it in bin, the the base of the project file, and the src file.

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class GameAudio extends JApplet {
    AudioClip song;

    public void init() {
        song = getAudioClip(getCodeBase(), "file: gameSong.wav"); 
        song.play();
    }
    public void start() {
        song.loop(); //Loop
    }
    public void stop() {
        song.stop(); //Stop the play when applet is closed
    }
}

Expected to be able to run audio as part of my applet, but this is the error I keep getting:

Exception in thread "main" java.lang.NullPointerException
    at java.applet.Applet.getCodeBase(Unknown Source)
    at GameAudio.init(GameAudio.java:9)
    at MathGameApp.main(MathGameApp.java:9)
Sambit
  • 7,625
  • 7
  • 34
  • 65
Lauren
  • 61
  • 2
  • 4
  • whats inside getCodeBase() method? Did you debug the method to see what it returns? – The Guest May 14 '19 at 18:39
  • I thought Applets were no longer being supported in Java. What Java are you using? Is this meant to be a web application of some sort? Usually we use some form or variant of class.getResource to load audio resources. – Phil Freihofner May 15 '19 at 20:25

0 Answers0