1

I'm trying to create some application to recognize faces, following this tutorial. However, whenever I run the code below, I get this error: Exception in thread "main" java.lang.NullPointerException

class DetectFaceDemo {
    public void run () {
        CascadeClassifier faceDetector = 
            new CascadeClassifier(getClass()
                  .getResource("/lbpcascade_frontalface.xml").getPath());
    }
}

Am I supposed to copy the xml file to some directory?

Thanks!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Adriano Valente
  • 529
  • 5
  • 15
  • 1
    Have you done this part of the tutorial? `Next, copy lbpcascade_frontalface.xml from opencv/data/ into the resources directory` – Stefan Sep 10 '14 at 16:05
  • Yes! It's in "C:\Users\Adriano\Documents\NetBeansProjects\LenaFaceDetection\build\classes\main\resources" directory. Is it the right one? – Adriano Valente Sep 10 '14 at 16:08
  • I believe it should be located in `C:\Users\Adriano\Documents\NetBeansProjects\LenaFaceDetection\build\classes\src\mai‌​n\resources`. I'm not sure if it should be in the `build` or `classes` directory though. – Stefan Sep 10 '14 at 16:09

2 Answers2

0

From your comment, I think you put your main/resources in the wrong directory (and you forgot the src part of the tree).

From the tutorial, you should have the following structure: src/main/java/<Java packages or classes>

You already have the src folder there. Create a main directory in src, and a resources directory in main. Put your XML file in the resources directory and you should be good to go.

Stefan
  • 1,433
  • 1
  • 19
  • 30
  • Thanks for the answer! I've put in `C:\Users\Adriano\Documents\NetBeansProjects\LenaFaceDetection\src\lenafacedetection` and (don't know why) it worked! – Adriano Valente Sep 10 '14 at 16:28
  • 2
    @AdrianoValente: no, don't put in the entire path, since if you do that, and then try to use the resource in a jar, your fried. Try Stefan's suggestion. – Hovercraft Full Of Eels Sep 10 '14 at 16:30
0

Put the following in the code to find the classpath and copy the xml and png files to this location.

System.out.println(System.getProperty("java.class.path"));
Bart
  • 1,268
  • 2
  • 12
  • 14