header photo

Solution for NullReferenceException

February 28, 2013

So, i checked the comments many readers posted and 've been getting this complaint  A LOT from MANY users, where most say something like this:

"I am having a NullRefrenceException error during runtime and the block of code it point to is below:

var faces =
grayframe.DetectHaarCascade(haar, 1.4, 4,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(25, 25))[0]; "

Part of the solution to a problem is understanding what the problem is.
                                                                                                                       - by Mahvish, The Great :P

SO WHAT does this error mean anyway?
Ans. It means that the variable you're trying to access actually is nowhere to be found!  meaning it can't be located right now!

So what is its solution?
Ans. naturally, making sure that your variable IS located where it should be!

yeah? wow great whatever - just How do i do that?
Ans. Well well, being impatient aren't you? hehehe, now  i may not be an expert, but from what I've learned, here's the PROBABLE solution to this problem. follow along the steps below and you'll get rid of the error! (most of the times...hopefully...i guess? :P )

SOLUTION Steps to Follow:
---------------------------------------------------------------------------------------
1- Identify which windows you are using:  32-bit OR 64-bit?

2- Identify WHICH installer of which version of EmguCV installer are you using:
so which VERSION are you using? version means EmguCv 2.0, EmguCv 2.2, EmguCv 2.3 or EmguCv 2.4 etc
and also which installer of it you using? the x86 installer(i.e the 32-bit)? or the x64 installer( i.e 64-bit)??
 
3- NOW check IF you are using the CORRECT .dll files in your project??
correct means that the .dll files corrosponding to your emgucv version and installer you're using,  for example,
if you used  EmguCV 2.something.x86  installer, then you HAVE to use the dll files located in its BIN folder!
Similarly IF you're using  EmguCV 2.something.x64  installer, then you HAVE to use the dll files located in its BIN folder!

WHY DO ALL THESE 3 STEPS?
because MOSTLY, that's the heart of the problem!
you see, even the .dll files of x64 and x86 installers of same version of Emgucv, e.g EmguCV.2.2 are DIFFERENT!
SO MAKE SURE YOU ARE USING THE RIGHT EmguCV version and installer on the right platform!


here's a helpful video for x64 windows users on configuring your EmguCV project to avoid errors:


4- check IF your code IS INDEED assigning some value to the variable you're calling:
Sometimes we make loops and conditions and forget to check if a variable being declared or called to is even being assigned some value at all at run-time or not. so see if you're misplaced your call in your code or not.

i'll add more checks and steps as i find more info :) you also, please if you know the solution still not mentioned here, DO share in comments :)

Go Back

Comment