Java程序打开摄像头 我的出现了异常  以下是代码 请大侠看看  谢谢
package com.lovo.Vido.Action;import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.Vector;
import javax.media.CaptureDeviceInfo;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.cdm.CaptureDeviceManager;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.AudioFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Action extends JFrame {
private static final Object South = null;
static Action frame;
private JPanel contentPane, p3, p4;
private JPanel panel;
public CaptureDeviceInfo device2;
public Player player2; Component comV, comVC, comA;
public static Player player = null;
private CaptureDeviceInfo device = null;
private MediaLocator locator = null;
static String str1 = "vfw:Logitech USB Video Camera:0";
static String str = "vfw:Microsoft WDM Image Capture (Win32):0";
static String aa = "javaSound audio capture";// 音频
static String bb = "DirectSoundCapture"; /**
 * Launch the application
 * 
 */
public static void main(String [] args){
EventQueue.invokeLater(new Runnable() {

/**
 * Runnable 提供的run方法
 */
public void run() {

frame = new Action();
frame.setVisible(true);
try {
frame.speaker();
} catch (Exception e) {
System.out.println("出现异常1:"+e.getMessage());
}
try {
frame.jbInit();
} catch (Exception e) {
System.out.println("系统出现异常2:"+e.getMessage());
}


}
});
}
private void speaker()throws Exception{
p3 = new JPanel();
p3.setLayout(new BorderLayout(0,0));
Vector deviceList = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.LINEAR,44100,16,2));

if(deviceList.size()>0){
device2 = (CaptureDeviceInfo)deviceList.firstElement();

}else{
System.out.println("抱歉找不到音频设备");
}
try{
player2 = Manager.createRealizedPlayer(device2.getLocator());
player2.start();
if((comA = player2.getControlPanelComponent())!=null){
p3.add(comA,"Center");
}
}catch(Exception e){
System.out.println("异常:"+e.getMessage());
}
contentPane.add(p3,South);
}
private void jbInit()throws Exception{
//初始化设备,str为设备驱动
p4 = new JPanel();
p4.setLayout(new BorderLayout(0,0));
p4.setOpaque(false);
device = CaptureDeviceManager.getDevice(str);
MediaLocator locatorss = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(locatorss);
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,new Boolean(true));

player.start();
comV=player.getVisualComponent();
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
if((comV)!=null){
p4.add(comV,"Center");
}
p4.setVisible(true);
contentPane.add(p4,"Center");
}

public Action(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,300);
contentPane = new JPanel();
contentPane.setOpaque(false);
contentPane.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0,0));
}

}
异常结果:出现异常1:null系统出现异常2:Cannot find a Player for :vfw://0
请说一下处理结果谢谢

解决方案 »

  1.   

    “出现异常1:null”
    —— 说明出现空指针了,后面再出现其他错误就更正常了。但是:
    } catch (Exception e) {
      System.out.println("出现异常1:"+e.getMessage());
    }这习惯非常不好,让人不知道问题位置,请修改为:
    } catch (Exception e) {
      System.out.println("出现异常1:"+e.getMessage());
      e.printStackTrace(); // 这句话非常非常重要!
    }然后看看是哪里出现空指针了。
      

  2.   

    出现null  是找不到 音频设备么?   我检查了下 不应该啊 ! 求 详细  谢谢
      

  3.   

    求啥详细啊,你把异常信息先完整弄出来再说,否则不能定位问题所在行。要完整堆栈信息:e.printStackTrace();
    不是仅仅这句:System.out.println("异常:"+e.getMessage());