Getting problem in accessing web cam
<p>I have written code in Java to access web cam,and to save image...
I am getting following exceptions :
Exception in thread "main" java.lang.NullPointerException
 at SwingCapture.(SwingCapture.java:40)
 at SwingCapture.main(SwingCapture.java:66)</p>

<p>how to remove this exceptions.</p>

<p>here is the code:</p>

<pre><code>import javax.swing.*;
</code></pre>

<p>import javax.swing.event.<em>;
import java.io.</em>;
import javax.media.<em>;
import javax.media.format.</em>;
import javax.media.util.<em>;
import javax.media.control.</em>;
import javax.media.protocol.<em>;
import java.util.</em>;
import java.awt.<em>;
import java.awt.image.</em>;
import java.awt.event.<em>;
import com.sun.image.codec.jpeg.</em>;</p>

<p>public class SwingCapture extends Panel implements ActionListener 
{
  public static Player player = null;
  public CaptureDeviceInfo di = null;
  public MediaLocator ml = null;
  public JButton capture = null;
  public Buffer buf = null;
  public Image img = null;
  public VideoFormat vf = null;
  public BufferToImage btoi = null;
  public ImagePanel imgpanel = null;</p>

<p>public SwingCapture() 
  {
    setLayout(new BorderLayout());
    setSize(320,550);</p>

<pre><code>imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);


String str1 = "vfw:iNTEX IT-308 WC:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();

try 
{
  player = Manager.createRealizedPlayer(ml);
  player.start();
  Component comp;

  if ((comp = player.getVisualComponent()) != null)
  {
    add(comp,BorderLayout.NORTH);
  }
  add(capture,BorderLayout.CENTER);
  add(imgpanel,BorderLayout.SOUTH);
} 
catch (Exception e) 
{
  e.printStackTrace();
}
</code></pre>

<p>}</p>

<p>public static void main(String[] args) 
  {
    Frame f = new Frame("SwingCapture");
    SwingCapture cf = new SwingCapture();</p>

<pre><code>f.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
  playerclose();
  System.exit(0);}});

f.add("Center",cf);
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);
</code></pre>

<p>}</p>

<p>public static void playerclose() 
  {
    player.close();
    player.deallocate();
  }</p>

<p>public void actionPerformed(ActionEvent e) 
  {
    JComponent c = (JComponent) e.getSource();</p>

<pre><code>if (c == capture) 
{
  // Grab a frame
  FrameGrabbingControl fgc = (FrameGrabbingControl)
  player.getControl("javax.media.control.FrameGrabbingControl");
  buf = fgc.grabFrame();

  // Convert it to an image
  btoi = new BufferToImage((VideoFormat)buf.getFormat());
  img = btoi.createImage(buf);

  // show the image
  imgpanel.setImage(img);

  // save image
  saveJPG(img,"\test.jpg");
}
</code></pre>

<p>}</p>

<p>class ImagePanel extends Panel 
  {
    public Image myimg = null;</p>

<pre><code>public ImagePanel() 
{
  setLayout(null);
  setSize(320,240);
}

public void setImage(Image img) 
{
  this.myimg = img;
  repaint();
}

public void paint(Graphics g) 
{
  if (myimg != null) 
  {
    g.drawImage(myimg, 0, 0, this);
  }
}
</code></pre>

<p>}</p>

<p>public static void saveJPG(Image img, String s)
  {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, null, null);</p>

<pre><code>FileOutputStream out = null;
try
{ 
  out = new FileOutputStream(s); 
}
catch (java.io.FileNotFoundException io)
{ 
  System.out.println("File Not Found"); 
}

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);

try 
{ 
  encoder.encode(bi); 
  out.close(); 
}
catch (java.io.IOException io) 
{
  System.out.println("IOException"); 
}
</code></pre>

<p>}</p>

<p>}</p>
