How to skip this error: Java.lang.arrayindexoutofboundsexception 0 imagej macro error?

The macro code is written to do analysis on several images. I noticed when an image is all black, I get this error. I need to add a piece of code that if this error happens, go to the next image instead of returning the error and stopping the language. How to add this error to if conditional statement?

Giving away a bit of code to this forum makes it easier for us to help you.
Generally speaking, depending on the nature of your image, we could assume that an ‘all black’ image means that your pixels all contain zero.
If that is the case, this code should do the trick (feel free to trim the code to the bare essentials)

use List.setMeasurements:

newImage("Untitled", "8-bit black", 100, 100, 1);
myID = getImageID();
//if resultOfProcessing == 0 then failed to analyse
	print("found result that would divide by zero");
}else if(resultOfProcessing==-1){
	print("window ID "+ myID + " is no longer available");
	print("found range of "+resultOfProcessing);

function processImage(myImageID){
		return -1;
	myMean = List.getValue("Mean");
	if(myMin==0 && myMax == 0 && myMean==0){
		//skip image or return a value to indicate no result is avaliable
		return 0;
		//process the image
		myRange = myMax / myMin;
		//return the result
		return myRange;
1 Like

You can use the getStatistics() function to determine if an image has all zero pixels, for example:

  run("Select None");
  getStatistics(nPixels, mean, min, max);
  if (mean==0 && min==0 && max==0)
     print("Image is all zeros: "+getTitle);
     print("Image is not all zeros: "+getTitle);
1 Like

@Wayne @eljonco
Thanks for your notes. There is a plugin that is called for each image. I don’t have access to the code of the plugin but for some images, I get this error. I noticed that for black images I get this error but after more investigation, I realized that there might be some other reasons that causes this error. I think the best strategy to skip this error and avoid the code to stop in the loop of let’s say 50 images, is that I add a piece of code that says if you get this error, do not stop and continue to the next image.
So instead of indirect skipping, I want to directly skip the error.

What is the name of the plugin? The ideal solution for this problem is to fix the bug that causes the exception. The bug can probably be fixed if you can provide a small macro that reproduces it. It may also be possible to fix the bug if you can provide the full text of the exception error, including the ImageJ version, the names of the Java methods in the call chain and line numbers.

1 Like

@Wayne Thank you for quick response. it is lpx. you can download it here:
it detects actin fibers in the cell cytoskeleton. This is the whole error I get:
The macro is a for loop and if you want to run it, you should have the same images I have to get this error. Is that what you need?

What does the line of macro code that calls the LPX plugin look like? The error message looks like it’s coming from one of the LPX plugins. It appears that source code for the LPX plugins is not available so there is no easy way to debug this problem. You will probably need to work around the error by not calling the LPX plugin when you detect an image that is known to cause the error. Can you upload an image that causes the error?

1 Like

@Wayne I get error for the first image:
but I do not get error for second image:
I first noticed that I will get error when the image is whole black which is easy to handle but now I see that for some reasons that are inside the plugin, I get this error.The error is from somewhere in the plugin code that I dont have access and it seems it would happen for some images (e.g. black images and so on). I am running the code to a bunch of images so the best way is to add sth to the code that does not drop out of the for loop once it gets to this error.

Could you please tell us the exact steps needed to reproduce the error? The LPX plugin has 25 different commands, many with multiple parameters. A small macro that reproduces the error using your first image would be ideal.

1 Like

run("Lpx Filter2d", "filter=lineFilters__ fftswapquadrants linemode=lineExtract__ giwsiter=5 mdnmslen=15 pickup=otsu abovethr=0 hystseg shavelen=5 dellen=5 draw=orgInt pregauss=-1");
Here is a piece of code to run lpx plugin.

I do not get an error when I run this code on either image. You could try using the Image>Type>8-bit command to transform the images to ones with standard grayscale LUTs. These two PNGs have custom LUTs that could potentially cause problems. You could also try installing a fresh copy of Fiji and a fresh copy of the LPX plugin. It is possible that one of then is corrupted.