Thresholded 32-bit Float Image Required! (an ImageJ macro Error)

As the title suggest, this is my error. However, I get the error randomly, sometimes my program works and sometimes it stops with this error.

Here is a bit of background info and the code in my program at which the error occurs:

getDimensions(width, height, channels, slices, frames);

setAutoThreshold("Default dark");
run("NaN Background");
run("Make Binary");

GradientX is originally a derivative image of which I want to threshold for pixels between a value and that value’s negative counter part, like the pixels from -infinity to -2, and from 2 to infinity . In order to do this, I apply an inverse for loop to change each pixels value to 1 over its original value. This makes the large values small, and the small values large. Then, I set my threshold using the variable Threshy, (which in this case would be 2 to get the pixels between 1/2 and -1/2).

However, My program sometimes works and thresholds the correct pixels, and other times I obtain this error. Does anyone have any idea as to what is causing it? I know for a fact that the error is happening at my Threshold stage.

Do note that gradient X is not a regular image with pixels ranging from 0-255, it is essentially a 2D array of random size with pixels values that may range from -infinity to infinity.

-Thank you for reading this thread, and please comment if you have any idea at all, even if you don’t have an exact solution! Through discussion, perhaps we can trigger something that will solve the problem!

Please let me know if you need additional information

I would say that the macro stucks at this command:

run("NaN Background") command

But why do you need this commands which actually causes the error:
run("Threshold...");? //-> Open the threshold dialog
setAutoThreshold(“Default dark”);`? //->Make an auto threshold

where you only need your selected threshold:

I wonder that this is working at all (if really all images are 32-bit!).