I have an Object variable that is constantly changing. More specifically, it is an OpenCV Mat() variable type. It is constantly changing in the background, being set to new values as the camera takes another picture. I am then trying to access the variable from a loop. I never explicitly set the variable to null, but I believe that when the variable is reset in the background loop it becomes null for an instant before it is set to a value.
I understand that, for Object variable types, java actually passes the reference to the variable, similar to a pointer in C/C++. To combat this, I am using the clone() function to make a variable that doesn't change as I access it at other points in the program. The problem is that sometimes the variable is null when I do the clone() function on it. I could check this using a if(variable != null) type of statement, but that doesn't guarantee that the variable doesn't change from the if statement to the cloning of the variable.
So, all that to ask, what would the correct way of handling this situation be such that I don't access a null variable on accident.
If the question was phrased poorly, or you want code included, let me know and I will include it. Thanks!