0

Im using code from an old book (2005) so I suspect that the problem is due to that. I am reading a chapter that explains how to play sounds and I am copying the code from the book to Eclipse but its spamming me with errors. The problems seem to start at this line:

g.drawString(" " + sample.getFormat().toString(), 10, 15*y++);

Here is the full code:

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class PlaySound extends Applet {
    String filename = "thrust.wav";
    AudioInputStream sample;
    
    private URL getURL(String filename) {
        URL url = null;
        try {
            url = this.getClass().getResource(filename);
        }
        catch (Exception e) {}
        return url;
    }
    
    public void init() {
        try {
            sample = AudioSystem.getAudioInputStream(getURL(filename));
            // create a sound buffer
            Clip clip = AudioSystem.getClip();
            // load the audio file
            clip.open(sample);
            // play the sound clip
            clip.start();
        }
        catch (MalformedURLException e) {
        } catch (IOException e) {
        } catch (LineUnavailableException e) {
        } catch (UnsupportedAudioFileException e) {
        } catch (Exception e) {}
    }
    
    // the paint event handles the screen refresh
    public void paint(Graphics g) {
        int y = 1;
        g.drawString("Sample file: " + filename, 10, 15*y++);
        g.drawString(" " + sample.getFormat().toString(), 10, 15*y++);
        g.drawString("  Sampling rate: " + (int)sample.getFormat().getSampleRate(), 10, 15*y++);
        g.drawString("  Sample channels: " + sample.getFormat().getChannels(), 10, 15*y++);
        g.drawString("  Encoded format: " + sample.getFormat().getEncoding().toString(), 10, 15*y++);
        g.drawString("  Sample size: " + sample.getFormat().getSampleSizeInBits() + "-bit", 10, 15*y++);
        g.drawString(" Frame size: " + sample.getFormat().getFrameSize(), 10, 15*y++);
    }
}

Edit here are the errors:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at PlaySound.paint(PlaySound.java:41)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at PlaySound.paint(PlaySound.java:41)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NewbChewbCS
  • 67
  • 2
  • 8
  • You state that it is "spamming" you with errors, but you have not shown a single error message. Please [edit] the question and fix. – DontKnowMuchBut Getting Better Sep 11 '20 at 23:41
  • 1
    Side note: this, `catch (Exception e) {}` is terrible and is the coding equivalent of flying blind. At least handle all exceptions with a `e.printStackTrace();` – DontKnowMuchBut Getting Better Sep 11 '20 at 23:42
  • Ok I added the errors – NewbChewbCS Sep 12 '20 at 00:10
  • A guess -- `sample` is null. You should test this. If so, then in all likelihood your path to the resource is not correct. – DontKnowMuchBut Getting Better Sep 12 '20 at 00:38
  • 1
    See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Sep 12 '20 at 02:53
  • 1
    Just noticed: `public class PlaySound extends Applet {` Ugh.. 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Sep 12 '20 at 02:56

0 Answers0