ImagePlus setROI() throws null pointer exception

Hello,

I am trying to write a plugin based off the existing Strahler Analysis plugin and I would like to select a rectangular ROI to work with. However, when I run the plugin in Eclipse, the setROI() function throws a NullPointerException. The code for this part is below. How can I fix this issue? My JRE version is 1.8.

165 ImagePlus imp= IJ.openImage(Strahler.class.getResource("/image.tif").getFile());
166 imp.setRoi(516, 517, 8, 6);

Here’s the error message I got:
Exception in thread “main” java.lang.NullPointerException
at dend.skel.Strahler.main(Strahler.java:166)

Thanks in advance.

Hello El -

I assume that you are modifying the original Strahler.java code, and
that imp.setRoi(516, 517, 8, 6); is indeed line 166 of your
version of Strahler.java.

The error message is telling you that imp is null, and therefore that
IJ.openImage() failed. Most likely, .getResource("/image.tif")
failed.

The analogous line of code in what might be the original Strahler.java
is line 171:

ImagePlus imp = IJ.openImage(Strahler.class.getResource("/skeleton2.tif").getFile());

and we see that the dendlab project has a skeleton2.tif file in its
resources directory.

You need to make sure that you include your image.tif file as a resource
in the jar you build that contains Strahler.class (or open the file from
elsewhere).

To debug this you might start by making sure that you can run the
original Strahler plugin from your eclipse environment. Then see if
you can run your modified Strahler.java, but still open the original
skeleton2.tif, and then finally check that you can replace skeleton2.tif
with your image.tif in the jar (or open it from elsewhere).

Thanks, mm

1 Like