-1

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!

4 Answers4

1

standard approach to such situations is:

final Type localCopy = globalVariable;
if (localCopy != null) {
    // work with localCopy variable
    // even if null is assigned to globalVariable - your localCopy will not be null
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • But what if the globalVariable equals null? How do you test for null, with the possibility that the variable will change to null within the if statement that you tested it in? – cessnageek Jan 17 '18 at 01:44
  • @cessnageek if globalVariable equals null, then null will be assigned to localVariable and local one will not change its value when global one is updated – Iłya Bursov Jan 17 '18 at 01:45
0

You should lock the thread, make the copy, release the thread.

Do you own the other thread?

  • This is for the FTC robotics competition. FTC provides the base application. My custom OpenCV bit updates the variable within the base app, and then another java class, which I assume runs in another thread, access and manipulates the variable by means of an intermediary Globals class, which has the variable defined as a static variable. Never explicitly worked with threads before in my life, so out of this whole mess, how would I tell if I own it? – cessnageek Jan 17 '18 at 01:47
0

You need to synchronize the access to your variable, i think that in your case simple lock should help:

synchronized (object) {
    // here you can do whatever you want with object, other calls synchronized will wait until you end
}
0

Seeing a code example would help to better understand the problem you are facing. Are you running multiple threads? If so, there are definitely some things to consider. Using volatile or synchronize may help: Difference between volatile and synchronized in Java

bmck2006
  • 122
  • 8
  • Read the comment on Vincenzo La Spesa's answer. That should help provide a little context. If you want the full code, what is the best way to post that? – cessnageek Jan 17 '18 at 01:47