Using the bio-formats importer in a groovy script

Hi all,

I’m new to the more advanced languages. My problem is that when using the bio-formats importer the image opens (two channels) but the instance of imp is apparently empty as I get this error.

"Java.lang.NullPointerException: Cannot invoke method setSlice() on null object".

What am I doing wrong? Do I maybe have to set the active image as imp somehow after bio-formats opens it?

#@File(label="Select a directory", style="directory") dir 
import ij.IJ
import ij.ImagePlus
dir.eachFileRecurse { file ->    
	filename =  dir.path+File.separator+file.name 
	if (filename.contains(".nd")){
	    	    imp = IJ.run("Bio-Formats Importer", "open="+filename+" color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
				imp.setSlice(1)
				IJ.run(imp, "Red", "")
				imp.setSlice(2)
				IJ.run(imp, "Blue", "")	
				imp.show()		
    }
}

Hi @BC3D,

try retrieving the image withimp = IJ.getImage(), as I’m not sure what the IJ.run method returns :wink:

#@File(label="Select a directory", style="directory") dir 
import ij.IJ
import ij.ImagePlus
dir.eachFileRecurse { file ->    
	filename =  dir.path+File.separator+file.name 
	if (filename.contains(".nd")){
	    	    IJ.run("Bio-Formats Importer", "open=["+filename+"] color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
				imp = IJ.getImage()
				imp.setSlice(1)
				IJ.run(imp, "Red", "")
				imp.setSlice(2)
				IJ.run(imp, "Blue", "")	
				imp.show()		
    }
} 

Cheers,
Robert

1 Like

Thanks @haesleinhuepf, that was the missing piece of the puzzle. I didn’t think to look in the IJ class … face palm.

1 Like