java error: Exception in thread “main” java.lang.NullPointerException

前提是你 提交于 2019-12-01 14:53:39
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!
}

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.

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?

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();

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);

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.

Victor

Remove this :

Processor processor = null;
DataSink datasink = null;

and replace with this :

Processor processor;
DataSink datasink;

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!