java错误:线程中出现异常��主要的�� Java语言lang.NullPointerException[英] java error: Exception in thread "main" java.lang.NullPointerException

问题描述

代码如下:

import java.awt.*;
import java.util.*;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.control.*;
import javax.media.format.*;

public class jmfcam05v
{
       DataSource dataSource;
       PushBufferStream pbs;

       Vector camImgSize = new Vector();
       Vector camCapDevice = new Vector();
       Vector camCapFormat = new Vector();

       int camFPS;
       int camImgSel;

       Processor processor = null;
       DataSink datasink = null;

       public static void main(String[] args)
       {
               jmfcam05v jmfcam = new jmfcam05v();
       }

       public jmfcam05v()
       {

               fetchDeviceFormats();
 camFPS = 30;    // framerate


               fetchDeviceDataSource();
               createPBDSource();
               createProcessor(dataSource);
               startCapture();
               try{Thread.sleep(20000);}catch(Exception e){}   // 20 seconds
               stopCapture();
       }

      boolean fetchDeviceFormats()
       {
               Vector deviceList = CaptureDeviceManager.getDeviceList(new VideoFormat(null));
               CaptureDeviceInfo CapDevice = null;
               Format CapFormat = null;
               String type = "N/A";

               CaptureDeviceInfo deviceInfo=null;boolean VideoFormatMatch=false;
               for(int i=0;i<deviceList.size();i++)
               {
                       // search for video device
                       deviceInfo = (CaptureDeviceInfo)deviceList.elementAt(i);
                       if(deviceInfo.getName().indexOf("vfw:")<0)continue;

                       Format deviceFormat[] = deviceInfo.getFormats();
                       for (int f=0;f<deviceFormat.length;f++)
                       {
                               if(deviceFormat[f] instanceof RGBFormat)type="RGB";
                               if(deviceFormat[f] instanceof YUVFormat)type="YUV";
                               if(deviceFormat[f] instanceof JPEGFormat)type="JPG";

                               Dimension size = ((VideoFormat)deviceFormat[f]).getSize();
                               camImgSize.addElement(type+" "+size.width+"x"+size.height);

                               CapDevice = deviceInfo;
                               camCapDevice.addElement(CapDevice);
                               //System.out.println("Video device = " + deviceInfo.getName());

                               CapFormat = (VideoFormat)deviceFormat[f];
                               camCapFormat.addElement(CapFormat);
                               //System.out.println("Video format = " + deviceFormat[f].toString());

                               VideoFormatMatch=true;  // at least one
                       }
               }
               if(VideoFormatMatch==false)
               {
                       if(deviceInfo!=null)System.out.println(deviceInfo);
                       System.out.println("Video Format not found");
                       return false;
               }

               return true;
       }


       void fetchDeviceDataSource()
       {
               CaptureDeviceInfo CapDevice =
(CaptureDeviceInfo)camCapDevice.elementAt(camImgSel);
               System.out.println("Video device = " + CapDevice.getName());
               Format CapFormat = (Format)camCapFormat.elementAt(camImgSel);
               System.out.println("Video format = " + CapFormat.toString());

               MediaLocator loc = CapDevice.getLocator();
               try
               {
                       dataSource = Manager.createDataSource(loc);
               }
               catch(Exception e){}

               try
               {
                       FormatControl formCont=((CaptureDevice)dataSource).getFormatControls()[0];
                       VideoFormat formatVideoNew = new
VideoFormat(null,null,-1,null,(float)camFPS);
                       formCont.setFormat(CapFormat.intersects(formatVideoNew));
               }
               catch(Exception e){}
       }

       void createPBDSource()
       {
               try
               {
                       pbs=((PushBufferDataSource)dataSource).getStreams()[0];
               }
               catch(Exception e){}
       }

       public void createProcessor(DataSource datasource)
       {
               FileTypeDescriptor ftd = new FileTypeDescriptor(FileTypeDescriptor.MSVIDEO);
               Format[] formats = new Format[] {new VideoFormat(VideoFormat.INDEO50)};
               ProcessorModel pm = new ProcessorModel(datasource, formats, ftd);
               try
               {
                       processor = Manager.createRealizedProcessor(pm);
               }
               catch(Exception me)
               {
                       System.out.println(me);
                       // Make sure the capture devices are released
                       datasource.disconnect();
                       return;
               }
       }

       private void startCapture()
       {
               // Get the processor's output, create a DataSink and connect the two.
               DataSource outputDS = processor.getDataOutput();
               try
               {
                       MediaLocator ml = new MediaLocator("file:capture.mpg");
                       datasink = Manager.createDataSink(outputDS, ml);
                       datasink.open();
                       datasink.start();
               }catch (Exception e)
               {
                       System.out.println(e);
               }
               processor.start();
               System.out.println("Started saving...");
       }

       private void pauseCapture()
       {
               processor.stop();
       }

       private void resumeCapture()
       {
               processor.start();
       }

       private void stopCapture()
       {
               // Stop the capture and the file writer (DataSink)
               processor.stop();
               processor.close();
               datasink.close();
               processor = null;
               System.out.println("Done saving.");
       }
}

错误:

运行:

Video device = vfw:Microsoft WDM Image Capture (Win32):0
Video format = YUV Video Format: Size = java.awt.Dimension[width=640,height=480] MaxDataLength = 614400 DataType = class [B yuvType = 32 StrideY = 1280 StrideUV = 1280 OffsetY = 0 OffsetU = 1 OffsetV = 3

javax.media.CannotRealizeException: Unable to provide all requested tracks
Exception in thread "main" java.lang.NullPointerException
        at jmfcam05v.startCapture(jmfcam05v.java:202)
        at jmfcam05v.(jmfcam05v.java:82)
        at jmfcam05v.main(jmfcam05v.java:64)

请帮我解决这个错误.我使用的是 windows vista 操作系统.

谁能建议我如何以 .3gp 格式存储文件?请帮助

推荐答案

Exception in thread "main" java.lang.NullPointerException
    at jmfcam05v.startCapture(jmfcam05v.java:202)

startCapture() 方法中 jmfcam05v.java 第 202 行的某些对象引用是 null,而代码正尝试使用点 . 运算符访问/调用它.

例如

SomeObject someObject = null;
someObject.doSomething(); // NullPointerException.

解决方案实际上很简单.只需通过实例化它来确保它不为空:

if (someObject == null) {
    someObject = new SomeObject();
}
someObject.doSomething(); // No NPE more!

...或者在访问/调用之前简单地做一个空检查:

if (someObject != null) {
    someObject.doSomething(); // No NPE more!
}

本文地址:https://www.itbaoku.cn/post/978683.html