pyimagej icon indicating copy to clipboard operation
pyimagej copied to clipboard

ij.py.from_java failed to convert java.lang.String

Open oeway opened this issue 3 years ago • 5 comments

Hi, I am trying to convert a java string to python string using ij.py.from_java but it give me an error.

script = """
#@output Object greeting
greeting = "hello"
"""
macro_result = ij.py.run_script(lang, script)
greeting = macro_result.getOutput("greeting")
greeting_py = ij.py.from_java(greeting)

The error is :

[java.lang.Enum.toString] [INFO] script:script.ijm = [[greeting], [hello]]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in org.scijava.convert.AbstractConvertService.convert()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in org.scijava.convert.AbstractDelegateConverter.convert()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in org.scijava.convert.AbstractConvertService.convert()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.convert.FileToDatasetConverter.convert()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.services.DefaultDatasetIOService.open()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.services.DefaultDatasetIOService.open()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.services.DefaultDatasetIOService.open()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.img.ImgOpener.openImgs()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.img.ImgOpener.createReader()

/srv/conda/envs/notebook/lib/python3.7/site-packages/_jpype.cpython-37m-x86_64-linux-gnu.so in io.scif.services.DefaultInitializeService.initializeReader()

Exception: Java Exception

The above exception was the direct cause of the following exception:

java.lang.NullPointerException            Traceback (most recent call last)
/tmp/ipykernel_538/2203817621.py in <module>
      7 macro_result = ij.py.run_script(lang, script)
      8 a = macro_result.getOutput("greeting")
----> 9 ij.py.from_java(a)

/srv/conda/envs/notebook/lib/python3.7/site-packages/imagej/__init__.py in from_java(self, data)
    577             except Exception as exc:
    578                 _dump_exception(exc)
--> 579                 raise exc
    580             return sj.to_python(data)
    581 

/srv/conda/envs/notebook/lib/python3.7/site-packages/imagej/__init__.py in from_java(self, data)
    570                 if self._ij.convert().supports(data, Dataset):
    571                     # HACK: Converter exists for ImagePlus -> Dataset, but not ImagePlus -> RAI.
--> 572                     data = self._ij.convert().convert(data, Dataset)
    573                     return self._dataset_to_xarray(data)
    574                 if self._ij.convert().supports(data, RandomAccessibleInterval):

java.lang.NullPointerException: java.lang.NullPointerException

oeway avatar Dec 14 '21 21:12 oeway

For some reason, SCIFIO's FileToDatasetConverter tries to convert the string to a Dataset by loading from a file path defined by the string. I wonder why it tries to convert to Dataset in the first place, but also the FileToDatasetConverter should not support the conversion for a file path that doesn't exist. @oeway what version of scifio is on your classpath?

imagejan avatar Dec 15 '21 07:12 imagejan

I honestly don't know, but maybe you can find some clue from this docker file I made for running it: https://gist.github.com/oeway/2d07e31a1ec769773463697c7c5542cb#file-dockerfile

oeway avatar Dec 15 '21 09:12 oeway

https://downloads.imagej.net/fiji/archive/20201104-1356/fiji-linux64.zip

It seems you're running an outdated Fiji installation which I believe ships an old version of SCIFIO that doesn't include the fix for StringToDatasetConverter and FileToDatasetConverter (although I didn't have time to check exactly which version).

Anyhow this issue might be part of the problem, but likely not the full solution, as you shouldn't be converting to Dataset at all here, in my opinion.

imagejan avatar Dec 15 '21 16:12 imagejan

https://downloads.imagej.net/fiji/archive/20201104-1356/fiji-linux64.zip

It seems you're running an outdated Fiji installation which I believe ships an old version of SCIFIO that doesn't include the fix for StringToDatasetConverter and FileToDatasetConverter (although I didn't have time to check exactly which version).

Anyhow this issue might be part of the problem, but likely not the full solution, as you shouldn't be converting to Dataset at all here, in my opinion.

Hi, I thought this fiji update command will get me to the latest version: https://gist.github.com/oeway/2d07e31a1ec769773463697c7c5542cb#file-dockerfile-L39

BTW @imagejan Where can I find the latest version? I would like to pin to a more recent version, and it seems the one I have is already the latest: https://downloads.imagej.net/fiji/archive/

oeway avatar Dec 15 '21 17:12 oeway

@oeway you are right, this seems to be the latest packaged version. The ImageJ core update sites have a newer state however, and only running the updater will get you the latest versions of everything.

/home/Fiji.app/ImageJ-linux64 --update add-update-site DeepImageJ https://sites.imagej.net/DeepImageJ

Sorry I missed this call to the updater. However, it seems you're just adding the update site, but not calling the full update (--update update) — unless I'm misunderstanding how the updater works.

imagejan avatar Dec 16 '21 08:12 imagejan

@oeway I'm fairly certain the SCIFIO bug causing this NPE is now long fixed, and released, and made available on the core update site. So I'm closing this. But if you still have this problem, definitely please reopen and I will try to reproduce it!

ctrueden avatar Jun 23 '23 16:06 ctrueden