# How to handle code producing an exception in OpenCV

**URL:** <https://forum.opencv.org/t/how-to-handle-code-producing-an-exception-in-opencv/7046>\
**Category:** Uncategorized\
**Created:** [January 9, 2022, 7:28pm UTC](https://forum.opencv.org/t/how-to-handle-code-producing-an-exception-in-opencv/7046 "2022-01-09T19:28:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rg0001](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rg0001](https://forum.opencv.org/u/rg0001)\
**Post date:** [January 9, 2022, 7:28pm UTC](https://forum.opencv.org/t/how-to-handle-code-producing-an-exception-in-opencv/7046/1 "2022-01-09T19:28:51Z")

</div>

I am trying to use the following code to extract blob center coordinates in an image. Problem is the marked line gives me an exception every time and when commented out runs without exceptions. Iv tried different permutations to shed further light but I am still stuck:

```auto
threshold(imgUndistorted, matBinImg, 15, 220, THRESH_BINARY);
			imshow("Binary Image:", matBinImg);
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params); 
detector->detect(matBinImg, binaryKeypoints);
			drawKeypoints(matBinImg, binaryKeypoints, img_binwith_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
(problem lines below)
**x_img = binaryKeypoints[0].pt.x;**
**y_img = binaryKeypoints[0].pt.y;**
Imshow("Test Window", img_binwith_keypoints);
waitKey(1);

I've also tried:

Point2f p = binaryKeypoints[i].pt;

float x = binaryKeypoints[i].pt.x;
float y = binaryKeypoints[i].pt.y;

```

Same exception  
Exception thrown: read access violation.  
binaryKeypoints.\_Mypair.\_Myval2.\_Myfirst was nullptr.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 9, 2022, 7:31pm UTC](https://forum.opencv.org/t/how-to-handle-code-producing-an-exception-in-opencv/7046/2 "2022-01-09T19:31:35Z")

</div>

please add the error AS TEXT, not as an image, thank you

then, without looking at it – what if it did not find any keypoints ?

---

<div class="post-metadata">

**Author:** ![rg0001](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rg0001](https://forum.opencv.org/u/rg0001)\
**Post date:** [January 9, 2022, 7:50pm UTC](https://forum.opencv.org/t/how-to-handle-code-producing-an-exception-in-opencv/7046/3 "2022-01-09T19:50:06Z")

</div>

That was it - thankyou

Now I need to add a filter to fix it. Any suggestions?

```auto
int objDetected = binKeypoints.size();

```
