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,请高手帮忙分析以下原因...
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,请高手帮忙分析以下原因...
解决方案 »
- 有时候他会卡住在line = reader.readLine()) != null
- jsp登录问题?
- Java J2SE /static 和 final 的区别
- javac 找不到 .class jdk1.1.8 系统 xp
- 请教javahelp中文full-text search的实现问题
- 大神们帮忙看一下!
- 如何在进程Process pcommand= Runtime.getRuntime().exec(cmd);中创建线程?
- 关于网页打印的方法??急!!在线等待!!!!高分求救!!!!
- 初学者的问题:我想将JBuilder包含的类导出来用(不用JBulider进行开发),我怎样才能最快地找到指定的类呢?谢谢!
- 请看:下面这个程序的运行结果是什么?为什么?
- 怎么用GUI实现QQ上的发送表情
- 关于JAVA的重写与重载问题
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
你的init方法是不是想覆盖JApplet里的呢?那个里 的init方法是空参数的,但你这里却带有参数,会不会是因为没有调用你的这个init而造成很多的对像没有初始化而产生空指针异常呢?
能不能说说你的这个使用的思路.就是这String[]参数是什么时候传进去的?
下面是赋值语句
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]);
}