在校生菜鸟求助,帮忙看下代码,为什么空指针异常?

韩香樟丸 2013-03-06 09:01:22
package com.test1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
public class JSoundDemo extends JFrame implements ActionListener{

public static void main(String[] args) {
// TODO Auto-generated method stub
JSoundDemo jsd=new JSoundDemo();
}

JPanel contentPanel;
JTextField jtf=new JTextField();
JPanel jPanel1=new JPanel();
JLabel jLabel1=new JLabel();
JButton jButton1=new JButton();
JButton jButton2=new JButton();
java.applet.AudioClip clip=null;
java.lang.ClassLoader cl=null;

public JSoundDemo(){

super();
this.setVisible(true);
this.setBounds(500, 250, 418,118);
this.setTitle("JSoundDemo");

jLabel1.setFont(new Font("Dialog",Font.BOLD,18));
jLabel1.setToolTipText("");
jLabel1.setText("waiting.....");

jButton1.setText("PLAY");
jButton1.addActionListener(this);
jButton2.setText("STOP");
jButton2.setEnabled(false);
jButton2.addActionListener(this);

jtf.setText(ClassLoader.getSystemResource("Sounds/12.au").toString());
//为什么这句显示空指针异常
contentPanel=(JPanel) this.getContentPane();
contentPanel.add(jtf,BorderLayout.NORTH);
jPanel1.add(jButton1);
jPanel1.add(jButton2);
contentPanel.add(jPanel1,BorderLayout.CENTER);
contentPanel.add(jLabel1,BorderLayout.SOUTH);

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jButton1){
System.out.println("hahh");
jButton1.setEnabled(false);
jButton2.setEnabled(true);
//实例化clip
try {
clip=java.applet.Applet.newAudioClip(new java.net.URL(jtf.getText()));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
clip.play();
jLabel1.setText("NowPlaying"+jtf.getText());
}
if(e.getSource()==jButton2){
System.out.println("woa");
jButton1.setEnabled(true);
jButton2.setEnabled(false);
clip.stop();
jLabel1.setText("waiting");
}
}
}

异常截图如下

...全文
138 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
韩香樟丸 2013-03-07
  • 打赏
  • 举报
回复
引用
把12.au放到src目录下,不要sounds目录试试;
现在更改了路径,没有报错,但是视频播放不出来,视频单独用win media player是播放的出的
李荣强 2013-03-06
  • 打赏
  • 举报
回复
引用 楼主 Randy_Orton2 的回复:
Java code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475package com.test1;import javax.swing……
 URL url= ClassLoader.getSystemResource("Sounds/12.au");
       System.out.println(url);
       String text=url.toString();
        jtf.setText(text);
我把你的代码分解了下,发现url是空的,由于对swing不是很了解,也不知道该怎么给你解释;估计是路径的问题,你可以把12.au放到src目录下,不要sounds目录试试; ClassLoader.getSystemResource("Sounds/12.au");这句改为ClassLoader.getSystemResource("12.au");
架构师梦 2013-03-06
  • 打赏
  • 举报
回复
引用 3 楼 Randy_Orton2 的回复:
貌似不行
把这句输出下看看: ClassLoader.getSystemResource("Sounds/12.au").toString()
韩香樟丸 2013-03-06
  • 打赏
  • 举报
回复
貌似不行
oZouBianBian 2013-03-06
  • 打赏
  • 举报
回复
ClassLoader.getSystemResource("./a.mp3")的结果为空,去调用toString的时候就报了空指针。 至于为什么为空,是因为你的资源路径错误
韩香樟丸 2013-03-06
  • 打赏
  • 举报
回复
自己认为这句代码有问题 jtf.setText(ClassLoader.getSystemResource("Sounds/12.au").toString());

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧