Script Editor - "Cannot invoke method getHierarchy() on null object"


a script that works, if the viewer is open, throws a script error, if it runs with closed viewer:

import qupath.lib.roi.*
import qupath.lib.objects.*
import qupath.lib.objects.classes.PathClass
import qupath.lib.objects.classes.PathClassFactory

def imageData = getCurrentImageData()
def hierarchy = imageData.getHierarchy()

The script error
INFO: Starting script at Wed Jan 08 19:35:08 CET 2020
ERROR: Error at line xx: Cannot invoke method getHierarchy() on null object

is for the line “def hierarchy = imageData.getHierarchy()”

Is it possible to prevent that?

The image has just annotations with the class “Tumor”

I don’t know how to fix it, though there are some other posts on the topic around the forum, but you don’t have a “currentImage” open, so you are trying to get the hierarchy of a null object.

Does the same thing happen when you “Run for project…” and only select the one image?

it happens only if i use “Run for project” with closed viewer.

Not sure what is happening with your project structure/files then. The script you posted above runs as written when an image is selected using Run for project…

If there are no objects, it still runs successfully and shows 0 objects.

If the script is run on the current image with no current image (not “for project” and with the Viewer closed) then it returns the following error, as intended.

i assume you are right and i did not start the project via “run for project” and instead just used run.
I was probably to tired to realize it ^-^

Thank you

1 Like