import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class audio extends JApplet implements ActionListener,ItemListener 
{
private AudioClip m_soundCurrent;
private AudioClip m_sound[]=new AudioClip[10];
private JButton m_buttonPlay, m_buttonLoop, m_buttonStop;
private JComboBox m_comboChoose;
int audioNumber;

public void init(String args[])//Build interfaces and set sounds
{ String[] choices=new String[10];

Container container = getContentPane();
container.setLayout(new FlowLayout());

for (int i=0; i<args.length; i++)
{ File f = new File(args[i]);
if(f.exists())  while(i<=10)  choices[i]=f.getName();
else System.out.println("Con't find the file"+ args[i]);
}
m_comboChoose = new JComboBox( choices );
m_comboChoose.addItemListener( this );
container.add( m_comboChoose);

m_buttonPlay = new JButton("Play");
m_buttonPlay.addActionListener( this );
container.add( m_buttonPlay );

m_buttonLoop = new JButton("Loop");
m_buttonLoop.addActionListener( this );
container.add( m_buttonLoop );

m_buttonStop = new JButton("Stop");
m_buttonStop.addActionListener( this );
container.add( m_buttonStop );

for(int i=0;i<=10;i++)
m_sound[i]=getAudioClip( getDocumentBase(),choices[i] );
m_soundCurrent = m_sound[1];
}//End of method: init

public void stop()//Stop playing sound
{    m_soundCurrent.stop();
}

public void itemStateChanged( ItemEvent e )
{ m_soundCurrent.stop();
m_soundCurrent =(m_comboChoose.getSelectedIndex()==0? m_sound[1] : m_sound[2]);
}//End of method: itemStateChanged

public void actionPerformed(ActionEvent e)
{       if( e.getSource()== m_buttonPlay )
m_soundCurrent.play();
else if( e.getSource() == m_buttonStop)
m_soundCurrent.stop();
else if( e.getSource() == m_buttonLoop)
m_soundCurrent.loop();
}//End of mathod: actionPerformed
}//End of class: J_Audio
打算做一个能够读取根目录下音频文件的的播放器,结果运行后出现java.lang.NullPointerException异常
我刚学JAVA,请高手帮忙分析以下原因...

解决方案 »

  1.   

    你的问题出在
    public void stop()//Stop playing sound 
    {    m_soundCurrent.stop(); 
    } m_soundCurrent是NULL.
    你把m_soundCurrent.stop(),放在m_soundCurrent = m_sound[1];下面就可以了!
    代码小改如下:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;public class audio extends JApplet implements ActionListener, ItemListener {
    private AudioClip m_soundCurrent; private AudioClip m_sound[] = new AudioClip[10]; private JButton m_buttonPlay, m_buttonLoop, m_buttonStop; private JComboBox m_comboChoose; int audioNumber; public void init(String args[])// Build interfaces and set sounds
    {
    String[] choices = new String[10]; Container container = getContentPane();
    container.setLayout(new FlowLayout()); for (int i = 0; i < args.length; i++) {
    File f = new File(args[i]);
    if (f.exists())
    while (i <= 10)
    choices[i] = f.getName();
    else
    System.out.println("Con't find the file" + args[i]);
    }
    m_comboChoose = new JComboBox(choices);
    m_comboChoose.addItemListener(this);
    container.add(m_comboChoose); m_buttonPlay = new JButton("Play");
    m_buttonPlay.addActionListener(this);
    container.add(m_buttonPlay); m_buttonLoop = new JButton("Loop");
    m_buttonLoop.addActionListener(this);
    container.add(m_buttonLoop); m_buttonStop = new JButton("Stop");
    m_buttonStop.addActionListener(this);
    container.add(m_buttonStop); for (int i = 0; i <= 10; i++)
    m_sound[i] = getAudioClip(getDocumentBase(), choices[i]);
    m_soundCurrent = m_sound[1];
    m_soundCurrent.stop();
    }// End of method: init public void itemStateChanged(ItemEvent e) {
    m_soundCurrent.stop();
    m_soundCurrent = (m_comboChoose.getSelectedIndex() == 0 ? m_sound[1]
    : m_sound[2]);
    }// End of method: itemStateChanged public void actionPerformed(ActionEvent e) {
    if (e.getSource() == m_buttonPlay)
    m_soundCurrent.play();
    else if (e.getSource() == m_buttonStop)
    m_soundCurrent.stop();
    else if (e.getSource() == m_buttonLoop)
    m_soundCurrent.loop();
    }// End of mathod: actionPerformed
    }// End of class: J_Audio
      

  2.   

    能不能说一下你怎么用的呢?
    你的init方法是不是想覆盖JApplet里的呢?那个里 的init方法是空参数的,但你这里却带有参数,会不会是因为没有调用你的这个init而造成很多的对像没有初始化而产生空指针异常呢?
      

  3.   

    我按照你的改过结果Applet小程序里面没有按钮的选项呢?
      

  4.   

    因为没写过JApplet,所以不知道在页面上如何去调用一个方法.因为你要传一个String[]参数的,所以你要在页面上显式的调用这个init(String[])方法.
    能不能说说你的这个使用的思路.就是这String[]参数是什么时候传进去的?
      

  5.   

    用Swing嘛,那个挺简单的,直接拖就可以了。
      

  6.   

    在init()方法里面赋值不可以的吗?
    下面是赋值语句
    String choices[] = new String[10]; for (int i = 0; i  < args.length; i++) 
    { //把所有文件路径赋予args数组
    File f = new File(args[i]); 
    if (f.exists()) 
    while (i  <= 10) 
    choices[i] = f.getName(); 
    else 
    System.out.println("Con't find the file" + args[i]);