Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV错误:空数组指针Java_Java_Opencv_Nullpointerexception - Fatal编程技术网

OpenCV错误:空数组指针Java

OpenCV错误:空数组指针Java,java,opencv,nullpointerexception,Java,Opencv,Nullpointerexception,我正在运行网络摄像头,试图实时检测物体,我已经运行了这段代码。它在video.main(video.java:92)-CvRect sq=cvBoundingRect(ptr,0)处给出错误但是我正在检查如果(ptr!=null)。我不明白为什么 CvMemStorage storage = CvMemStorage.create(); CvSeq contours = new CvContour(null); noOfContors = cvFindContours(img

我正在运行网络摄像头,试图实时检测物体,我已经运行了这段代码。它在
video.main(video.java:92)
-
CvRect sq=cvBoundingRect(ptr,0)处给出错误但是我正在检查
如果(ptr!=null)
。我不明白为什么

CvMemStorage storage = CvMemStorage.create();
CvSeq contours = new CvContour(null);          
noOfContors = cvFindContours(imgbin, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, new CvPoint(0,0));


 for (ptr = contours;  ptr != null; ptr = ptr.h_next()) {     
    if(ptr != null){
         CvRect sq = cvBoundingRect(ptr, 0);
         if(sq.height()*sq.width() > minAreaa && sq.height()* sq.width() < maxAreaa){
            p1.x(sq.x());
            p2.x(sq.x()+sq.width());
            p1.y(sq.y());
            p2.y(sq.y()+sq.height());
            cvRectangle(img1, p1, p2, CV_RGB(255, 0, 0), 2, 8, 0);
        } 
    }
}

通过在
for
循环中添加附加条件解决了问题,但仍不知道这是否是正确的处理方法:

CvSeq contours1 = new CvContour(null);
for (ptr = contours;  ptr != null && cvFindContours(imgbin, storage, contours1, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, new CvPoint(0,0)) != 0; ptr = ptr.h_next()){ .....

}

请改用以下方法:

 for (ptr = contours; ptr != null && !ptr.isNull(); ptr = ptr.h_next()) {

这个!ptr.isNull()修复空指针错误。

错误提到
null
数组指针。
 for (ptr = contours; ptr != null && !ptr.isNull(); ptr = ptr.h_next()) {