Reputation: 15
I was hoping someone would be able to help me with a problem I've been having with an application I'm developing that makes use of a webcam in java with JMF media library.
The problem I am having is I can run the webcam ok in an application by itself with this class here
import java.awt.BorderLayout;
import java.util.Vector;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FormatControl;
import javax.swing.JFrame;
import javax.swing.JButton;
public class WebcamClass{
CaptureDeviceInfo cam;
MediaLocator locator;
Player player;
FormatControl formatControl;
public WebcamClass(){
try{
// List out available Devices to Capture Video.
Vector<CaptureDeviceInfo> list = CaptureDeviceManager.getDeviceList ( null );
System.out.println(list);
// Iterating list
for(CaptureDeviceInfo temp : list){
// Checking whether the current device supports VfW
// VfW = Video for Windows
if(temp.getName().startsWith("vfw:"))
{
System.out.println("Found : "+temp.getName().substring(4));
// Selecting the very first device that supports VfW
cam = temp;
System.out.println("Selected : "+cam.getName().substring(4));
break;
}
}
System.out.println("Put it on work!...");
// Getting the MediaLocator for Selected device.
// MediaLocator describes the location of media content
locator = cam.getLocator();
if(locator != null){
// Create a Player for Media Located by MediaLocator
player = Manager.createRealizedPlayer(locator);
if(player != null){
// Starting the player
player.start();
// Creating a Frame to display Video
JFrame f = new JFrame();
f.setTitle("Test Webcam");
f.setLayout(new BorderLayout());
// Adding the Visual Component to display Video captured by Player
// from URL provided by MediaLocator
f.add(player.getVisualComponent(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
}catch(Exception e){
System.out.println(e);
}
}
}
However when I put it into my GUI application where I would like to run it from I keep getting "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" when i press the button to turn the camera on.
I know it isn't picking up the webcam device but i can't understand why as it does when i'm not trying to embed it in my GUI.
I have the JMF.jar in my libraries folder as well.
Any help would be greatly appreciated.
Upvotes: 0
Views: 3404
Reputation: 1
cam.getLocator();
is throwing the exception. Your list is not populating with any devices.
Upvotes: 0
Reputation: 325
Without more info on your NullPointerException
it is impossible to say what is causing the problem. In the stack trace for the exception, you should identify the line in the code you wrote that triggers the exception.
Without any more information, my guess is you don't have an ActionListener
registered to the JButton
that should start the camera.
Upvotes: 1