Search code examples
javaswingaudionullpointerexceptionjapplet

How did I get a NullPointerException? Working with JApplet


It's been a long time since I have worked with java and I forgot how to deal with a NullPointerException. The only thing I can think of is line 8. I'm not sure if that's how I'm supposed to retrieve an audio file from the same folder the java file is located in.

This is my first time working with AudioClip. If that is the problem, what is the correct way to do it? Thank you in advance for any help/tips.

If there is any other piece of information that I can provide, to help you help me, please let me know. :)

[Purpose of this code is to create three buttons that will allow the user to play,loop, and stop the music]

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

public class ProgAsThree extends JApplet
{
   private AudioClip music = Applet.newAudioClip(getClass().getResource("Music.mp3"));
   private JButton jbtPlay = new JButton("Play");
   private JButton jbtLoop = new JButton("Loop");
   private JButton jbtStop = new JButton("Stop");

   public ProgAsThree()
   {
      JPanel panel = new JPanel();
      panel.add(jbtPlay);
      panel.add(jbtLoop);
      panel.add(jbtStop);
      add(panel);

      jbtPlay.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            music.play();
         }
      });

      jbtLoop.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            music.loop();
         }
      });

      jbtStop.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            music.stop();
         }
      });      

     } 
 }                  

java.lang.NullPointerException
at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:65)
at java.applet.Applet.newAudioClip(Applet.java:311)
at ProgAsThree.<init>(ProgAsThree.java:8)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:722)

Solution

  • public void init() {
        // ..
        URL pathToClip  = new URL(getCodeBase(), "Music.mp3");
        music = Applet.newAudioClip(pathToClip);
    

    Last time I checked, the AudioClip could not handle MP3 format (out of the box). See the Java Sound info. page for the fix.