-2

I am new to Java and I want to make an audio sequencer. So I've started with a class for sounds and I have this:

public class Sb {
public static AudioInputStream audioInputStream;
public static File file;
public static Clip clip;

Sb(String a){
    try {
    file = new File(a);
    audioInputStream = AudioSystem.getAudioInputStream(file);
    } 
    catch (UnsupportedAudioFileException u) {
    }
    catch(IOException i) {
    }
    catch(NullPointerException n) {
    }
    
}

public static void play(){
    clip.start();
}  
}

I declare an instance like:

public static Sb hats = new Sb("file path");

And when I run hats.play() I get:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Any ideas how I can make this class work?

Thanks!

Edit:

Modified Constructor:

Sb(String a) throws UnsupportedAudioFileException, IOException, LineUnavailableException {

    file = new File(a);
    audioInputStream = AudioSystem.getAudioInputStream(file);
    clip = AudioSystem.getClip();
  
}

But when I declare it now I get an error on this line:

 public static Sb hats = new Sb("file path");

Error:

"unreported exception UnsuportedAudioFileException must be caught or declared to be thrown
Community
  • 1
  • 1
  • 2
    Well you can start by not suppressing exceptions in your constructor - you're swallowing everything without even logging the problem. That's a very bad idea. It would be better if you *at least* logged - and ideally just declare that your constructor can throw `UnsupportedAudioFileException` and `IOException`. – Jon Skeet Jun 10 '14 at 16:36
  • What line is the NPE occurring on? – Paul Richter Jun 10 '14 at 16:36
  • Can you paste the full stacktrace? It seems like you are calling `clip.start()` without initializing `clip`. – Christian Tapia Jun 10 '14 at 16:36
  • @PaulRichter well there is only one line in `play()` so I would guess that one. – clcto Jun 10 '14 at 16:36
  • @clcto You're right, missed that. – Paul Richter Jun 10 '14 at 16:37
  • If you want to call clip.start() you must set clip to a valid Clip object. it appears that clip is uninitialized (and thus null). – DwB Jun 10 '14 at 16:40
  • Why are you using `static` on everything? Do you understand what its purpose is? – ajb Jun 10 '14 at 16:42
  • I didnt quite got this, but I am learning. Any solution to my situation ? – user3719358 Jun 10 '14 at 16:46

1 Answers1

2

Your clip is null, by default instance variable are initialised to null.

EDIT

You need to wrap your client code with try-catch

try{ Sb hats = new Sb("file path"); }catch(UnsupportedAudioFileException e){ // have some log statement }

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
  • I get "unreported exception UnsuportedAudioFileException must be caught or declared to be thrown" on the line where I declare an instance of Sb class. – user3719358 Jun 10 '14 at 16:51
  • I've worked it out. But now the sample plays only once. Any ideas ? – user3719358 Jun 10 '14 at 17:16
  • @user3719358 - One idea is that you need to learn to use your IDE's debugger, set breakpoints, single-step, etc. Otherwise you will always have to ask other people to debug your code ... and that doesn't work. – Stephen C Jun 10 '14 at 22:43