Search code examples
javaopencvgetresource

NullPointerException using getResource


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!


Solution

  • 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.