1

我正在尝试让一个测试程序运行,它可以从我的硬盘驱动器中播放声音文件,但我继续得到NullPointerException. 这是到目前为止的代码,我主要从代码中的梦想中提取出来:

package ForSelf;

import java.applet.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.*;
import java.net.*;

public class AudioTest extends JApplet{
JPanel playerPanel;
JButton playSound, stopSound;
public class Sound // Holds one audio file
{
  private AudioClip song; // Sound player
  private URL songPath; // Sound path

  Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL
        song = Applet.newAudioClip(songPath); // Load the Sound
     }catch(Exception e){
         e.printStackTrace();
         //e.getMessage();
     } // Satisfy the catch
  }

  public void playSound(){
     song.loop(); // Play 
  }
  public void stopSound(){
     song.stop(); // Stop
  }
  public void playSoundOnce(){
     song.play(); // Play only once
  }
}

public void init(){
  Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav");
  Container c = getContentPane();

  c.setBackground(Color.white);
  c.setLayout(null);

  playerPanel = new JPanel();
  playSound = new JButton("Play");
  stopSound = new JButton("Stop");

  playerPanel.add(playSound);
  playerPanel.add(stopSound);
  c.add(playerPanel);

  testsong.playSound();
}

public void paint(Graphics g){
    super.paint(g);
    playerPanel.setLocation(0, 0);
    playerPanel.setSize(300, 300);
}

public void actionPerformed(ActionEvent e){

}
}

JComponents 将在稍后实现以播放和停止歌曲文件,我不确定它是否只是我的文件路径或什么,所以任何帮助将不胜感激。

更新后的代码如下:

package ForSelf;

import java.applet.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.*;
import java.net.*;

public class AudioTest extends JApplet{
JPanel playerPanel;
JButton playSound, stopSound;
public class Sound // Holds one audio file
{
  private AudioClip song; // Sound player
  private URL songPath; // Sound path

  Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL
        song = Applet.newAudioClip(songPath); // Load the Sound
     }catch(Exception e){
         e.printStackTrace();
         //e.getMessage();
     } // Satisfy the catch
  }

  public void playSound(){
     song.loop(); // Play 
  }
  public void stopSound(){
     song.stop(); // Stop
  }
  public void playSoundOnce(){
     song.play(); // Play only once
  }
}

public void init(){
    **String directory = System.getProperty("user.dir") + System.getProperty("file.separator");
    String puzzleSolutionGet = directory + "PuzzleSolutionGet";
    Sound testsong = new Sound(puzzleSolutionGet);**

  //Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav");

  Container c = getContentPane();

  c.setBackground(Color.white);
  c.setLayout(null);

  playerPanel = new JPanel();
  playSound = new JButton("Play");
  stopSound = new JButton("Stop");

  playerPanel.add(playSound);
  playerPanel.add(stopSound);
  c.add(playerPanel);

  testsong.playSound();
}

public void paint(Graphics g){
    super.paint(g);
    playerPanel.setLocation(0, 0);
    playerPanel.setSize(300, 300);
}

public void actionPerformed(ActionEvent e){

}
}

但是,当我运行它时,我遇到了以下异常:

java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(URL.java:574)
    at java.net.URL.<init>(URL.java:464)
    at ForSelf.AudioTest$Sound.<init>(AudioTest.java:23)
    at ForSelf.AudioTest.init(AudioTest.java:45)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)
java.lang.NullPointerException
    at ForSelf.AudioTest$Sound.playSound(AudioTest.java:32)
    at ForSelf.AudioTest.init(AudioTest.java:62)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)

这可能比以前提供更多信息。我什至将文件移动到我的桌面以便更快地访问,但我确定位置不是问题。

4

2 回答 2

1

您需要使用正斜杠而不是反斜杠。我使用 linux 路径名(“/home/username/song.wav”)在我的 linux 机器上运行了你的代码,它没有抛出任何异常(只是一个警告)。

我很确定您需要使用:

Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav");

请记住,Windows 接受正斜杠和反斜杠作为目录分隔符。

但是,如果这对您不起作用,请尝试使用 System.getProperty("user.dir") 和 System.getProperty("file.separator") 方法创建当前目录的字符串,然后您只需要最后连接您的文件名:

String directory = System.getProperty("user.dir") + System.getProperty("file.separator");
String puzzleSolutionGet = directory + "PuzzleSolutionGet";
Sound testsong = new Sound(puzzleSolutionGet);

这样您的代码将在任何操作系统上运行。

它还将为您节省大量时间来切换您认为抛出异常的行并尝试调试代码,单步执行每一行,直到您看到抛出的异常。如果您还没有使用过调试器,这里是 IBM 提供的一个很酷的链接,它解释了基础知识:

http://www.ibm.com/developerworks/library/os-ecbug/

于 2012-04-25T15:12:58.270 回答
0
Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav");
...
Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL

路径需要相对于codebase, 并使用正斜杠/。EG 如果codebase指向:

"C:\\Users\\MyName\\lib"

构造函数应该看起来更像:

Sound testsong = new Sound("../Music/PuzzleSolutionGet.wav");
于 2012-04-25T14:46:59.883 回答