Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Exception in thread "main" java.lang.NullPointerException

    Hello, im new in this forum and programming in java, and i have a problem:
    at school we are programming this code to initialize the webcam and show it on the screen, but it doesn´t work :C this is a code that did run in another computer, but i cant see what's wrong with it . My webcam does work good, and the driver is already installed. Hope you can help me, im a noob u.u. Here is the code:

    package webcam;

    import java.awt.BorderLayout;
    import javax.media.CaptureDeviceInfo;
    import javax.media.CaptureDeviceManager;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.Player;
    import javax.media.Processor;
    import javax.swing.JFrame;



    public class Webcam extends JFrame {

    private static Player player;
    private MediaLocator localizador;
    private Processor p;
    private CaptureDeviceInfo dispositivo=null;
    private static String source="vfw:Microsoft WDM Image Capture (Win32):0";

    public Webcam(){
    setLayout(new BorderLayout());
    setSize(500,400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Camara Web");
    player=null;
    localizador=null;
    dispositivo=CaptureDeviceManager.getDevice(source) ;
    localizador=dispositivo.getLocator();
    iniciarCaptura();
    }

    private void iniciarCaptura(){
    try{
    player=Manager.createRealizedPlayer(localizador);
    player.start();
    if(player.getVisualComponent()!=null){
    add(player.getVisualComponent(), BorderLayout.CENTER);
    }
    }
    catch(Exception e){
    System.err.println(e.toString());
    }
    }

    public static void main(String[] args) {
    Webcam camara=new Webcam();
    Webcam.setDefaultLookAndFeelDecorated(true);
    camara.setVisible(true);
    }
    }


    //THE ERROR IS THE NEXT ONE
    Exception in thread "main" java.lang.NullPointerException
    at webcam.Webcam.<init>(Webcam.java:35)
    at webcam.Webcam.main(Webcam.java:53)
    Java Result: 1

    please help! :C


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    You are correct to assume "CaptureDeviceManager.getDevice(source);" is resulting in null.
    Based on the api for that method (JMF 2.0 API (03/10/01): Class CaptureDeviceManager "Returns null if the specified device could not be found."
    So the question now is why can the device not be found?
    I don't have the time at the moment to do the google searching for you, but the next step in debugging would be to get java to retrieve a list of devices attached to your computer. There is probably some topics out there regarding this if you search around (or perhaps someone else on the forum already knows). Get the list, and then loop through the list and print out the source names. This could be something as simple as you are using the wrong source name in the getDevice() method. But, first we want to make sure java recognizes a webcam is attached in the first place.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM