我正在使用一本旧书(2005年)中的代码,因此我怀疑问题是由于此而引起的。我正在阅读一章,该章解释了如何播放声音,并且正在将代码从书中复制到Eclipse,但是它使我充满了错误。问题似乎始于此行:
g.drawString(“” + sample.getFormat()。toString(),10,15 * y ++);
这是完整的代码:
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++);
}
}
在这里编辑是错误:
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)