制作Swing应用程序,用户使用单选按钮选择音频文件并使用“播放”按钮进行播放。 GUI类从自定义音频处理程序类调用该方法。音频文件位于名为audio的包中。选择声音并且用户单击“播放”按钮后,将引发以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.audio.AudioStream.<init>(AudioStream.java:63)
at my.quotesbutton.Player.fear(Player.java:18)
at my.quotesbutton.QuotesButtonUI.jButton3ActionPerformed(QuotesButtonUI.java:221)
at my.quotesbutton.QuotesButtonUI.access$000(QuotesButtonUI.java:16)
at my.quotesbutton.QuotesButtonUI$1.actionPerformed(QuotesButtonUI.java:66)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
GUI类代码如下:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton1.isSelected()){
Player play = new Player();
try {
play.fear();
} catch (IOException ex) {
Logger.getLogger(QuotesButtonUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (jRadioButton2.isSelected()){
}
else if (jRadioButton3.isSelected()){
}
else if (jRadioButton4.isSelected()){
}
else if (jRadioButton5.isSelected()){
}
else if (jRadioButton6.isSelected()){
}
else {
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
The code for the audio handler class as follows:
package my.quotesbutton;
import java.io.*;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Player{
public void fear() throws IOException{
InputStream inputStream = getClass().getResourceAsStream("audio\\fear.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
}
答案 0 :(得分:0)
您似乎正在将null
传递给AudioStream
构造函数。您传递的值是通过getClass().getResourceAsStream()
获得的。
来自Java Class
javadoc:
public InputStream getResourceAsStream(String name)
...
<强>返回:强>
如果找不到具有此名称的资源,则为InputStream
对象或null
因此,这可能返回null
的方式是找不到该文件。问题出在您的文件路径中。你传递的路径是相对的。尝试查找应用程序工作目录的位置并更正相对于它的路径。
编辑:您可以通过System.getProperty("user.dir")
获取工作目录。