The reasons for the problem can be:-
1. Webcam drivers are corrupted or they are not properly installed.
2. Webcam is busy in some other application.
3. Webcam is not connected properly to the system.
4. You connected/pluggedin the webcam after launching the class. Connect webcam before you launch the class.