java 错误:线程“main”中出现异常java.lang.NullPointerException

发布于 2024-08-19 09:33:29 字数 6814 浏览 6 评论 0原文

这是代码:

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 格式存储文件?请帮助

Here is code:

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.");
       }
}

Error:

run:

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)

please help me with this error.i am using windows vista OS.

can anyone suggest me how to store files in .3gp format?please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无敌元气妹 2024-08-26 09:33:29
线程“main”中的异常 java.lang.NullPointerException
    在 jmfcam05v.startCapture(jmfcam05v.java:202)

当代码尝试访问/调用时,jmfcam05v.java 第 202 行(在 startCapture() 方法内)的某些对象引用为 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!
}
Exception in thread "main" java.lang.NullPointerException
    at jmfcam05v.startCapture(jmfcam05v.java:202)

Some object reference at line 202 of jmfcam05v.java, inside the startCapture() method, is null while the code is trying to access/invoke it using the dot . operator.

E.g.

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

The solution is actually easy. Just make sure that it's not null by either instantiating it:

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

... or by simply doing a nullcheck before accessing/invoking:

if (someObject != null) {
    someObject.doSomething(); // No NPE more!
}
魔法唧唧 2024-08-26 09:33:29

NPE很简单。 startCapture 方法中的其中一行

MediaLocator ml = new MediaLocator("file:capture.mpg");
datasink = Manager.createDataSink(outputDS, ml);

抛出 CannotRealizeException。因此,datasink 未初始化,如果您稍后尝试在 stopCapture 中关闭它,它仍然是 null,这会导致 NPE。

为了避免 NPE:测试,如果在调用方法之前 datasink 不为 null。

编辑

,您可以从构造函数中删除应用程序逻辑并将其移至主方法吗?构造函数是用来构造一个对象的,主要是初始化类成员,除此之外没有其他作用。 Java 类应该以大写字母开头,这有助于人们(和我们)理解代码。

The NPE is easy. One of the lines

MediaLocator ml = new MediaLocator("file:capture.mpg");
datasink = Manager.createDataSink(outputDS, ml);

in method startCapture throws a CannotRealizeException. So datasink is not initialized and if you try to close it later in stopCapture, it's still null and that causes the NPE.

To avoid the NPE: test, if datasink isn't null before calling a method on it.

EDIT

and could you PLEASE remove the application logic from the constructor and move it to the main method. A constructor is for constructing an object, mainly for initializing class members and nothing else. And Java classes should start with a capital letter, that helps people (and us) understanding the code.

最丧也最甜 2024-08-26 09:33:29

糟糕的异常处理是罪魁祸首。

最有可能的是,processor 成员未初始化。如果没有处理器,看起来你就无法做任何有用的事情。因此,让异常飞翔,在此时终止你注定要失败的程序,而不是“吞掉”它并愉快地继续。

另外,如果您要处理异常并继续前进,至少要正确打印它们:ex.printStackTrace(),而不是System.out.println(ex)

但最好在方法中添加 throws 子句,并且不捕获任何异常,因为您无法从异常中恢复。


也许 Indeo 编解码器存在于您的 Windows XP 机器上,但在您的 Vista 计算机上不可用。 (更新事实上,Indeo 不受支持并且与 Vista 不兼容.)这会阻止处理器成功创建,并且您的程序从那时起就注定失败。有没有办法测试特定的 FileTypeDescriptor 或 VideoFormat 在运行时是否有效?

Poor exception handling is to blame here.

Most likely, the processor member isn't getting initialized. If there isn't a processor, it looks like you can't do anything useful. So let the exception fly, terminating your doomed program at that point, instead of "swallowing" it and blithely continuing on.

Also, if you are going to eat exceptions and soldier on, at least print them properly: ex.printStackTrace(), instead of System.out.println(ex).

But it would be much better to add throws clauses to your methods, and not catch any exceptions, since you cannot recover from them.


Perhaps the Indeo codec was present on your Windows XP box, but isn't available on your Vista machine. (Update: In fact, Indeo is not supported and not compatible with Vista.) That prevents the processor from being created successfully, and your program is doomed from that point. Is there a way to test whether a particular FileTypeDescriptor or VideoFormat is valid at runtime?

不必在意 2024-08-26 09:33:29

看起来好像这一行:

processor = Manager.createRealizedProcessor(pm);

抛出 CannotRealizeException,导致 processor 为 null 并随后导致 NPE。至于抛出什么异常,这似乎与您的数据和 JMF 的使用有关。

一般来说,在异常上使用 System.out.println() 是不好的,因为这样你会丢失堆栈跟踪,而这通常是调试时最重要的信息。反而。使用异常.printStackTrace();

It looks as though this line:

processor = Manager.createRealizedProcessor(pm);

Throws a CannotRealizeException, causing processor to be null and leading to the NPE later on. As for what that exception is thrown, that seems to have to do with your data and use of the JMF.

Generally, it's bad to use System.out.println() on exceptions because then you lose the stack trace, which is often the most important information for debugging. Instead. use exception.printStackTrace();

分開簡單 2024-08-26 09:33:29

数据接收器从未被初始化。

当你尝试调用

datasink.close();

它时,它说 datasink 为空。

确保您的代码实际上到达并处理第 #176 行

datasink = Manager.createDataSink(outputDS, ml);

datasink is never being initialized.

When you try to call

datasink.close();

Its saying that datasink is null.

Make sure that your code is actually getting to and processing line #176

datasink = Manager.createDataSink(outputDS, ml);
貪欢 2024-08-26 09:33:29

通过查看private void startCapture()方法,我猜processor变量是NULL,因为这是唯一不在中的东西。 code>try-catch 块。

By looking at the private void startCapture() method I guess the processor variable is NULL as this is the only stuff that is not in a try-catch block.

滥情空心 2024-08-26 09:33:29

删除此:

Processor processor = null;
DataSink datasink = null;

并替换为:

Processor processor;
DataSink datasink;

Remove this :

Processor processor = null;
DataSink datasink = null;

and replace with this :

Processor processor;
DataSink datasink;
滿滿的愛 2024-08-26 09:33:29

这是因为 createRealizedProcessor 无法在 VedioCapture 设备上工作。
问题出在 Microsoft WDM 图像设备上。

This is because of createRealizedProcessor is not able to work on VedioCapture device.
Issue is with Microsoft WDM Image device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文