I’m trying to get the image of an item (shortcut).
I’m trying to get the image of an item (shortcut). The item is in a folder. I often got a NullPointerException. But when I open the folder before and close it, no exception is thrown. This is the relevant script excerpt:
…
10 item = LL.getItemById(itemid);
11 if (item == null) { return; }
12 image = item.getImage();
…
The exception points to line 12. Could it be, that the image in RAM isn’t rendered at this time. And opening the folder renders the image? Pierre Hébert how can I check if a rendered image exists?
I could use item.getCustomIcon() or item.getDefaultIcon() but then the mirror or scaling of properties isn’t applied.
]]>
< ![CDATA[
Yes, the getImage returns the live image, so it is not yet loaded in memory if it hasn’t been displayed before.
The getImage returns null in this case, just add a if(image==null) before using it
]]>
< ![CDATA[
The problem is, that I have this if in line 13, but the script stops in line 12 with Java.lang.nullpointerexception.
]]>
< ![CDATA[
How about this?
Because I think the item is NOT NULL but only its IMAGE is NULL
item = LL.getItemById(itemid);
if (item.getImage() == null) { return; }
image = item.getImage();
]]>
< ![CDATA[
The issue is in the implementation of getImage. TrianguloY is partly right, the item is not yet displayed and its view is not yet loaded, so there is no image to return. But the NullPointerException is thrown from the java code, this is not a bug in the script but in the launcher, which should check that some internal data are not yet ready. I will fix this (actually I though I already fixed this in the past, strange?) but in the meantime maybe try this (not tested, not sure it will work):
var image;
try {
image = item.getImage();
} catch(e) {
image = null;
}
if(image != null) {
// use it
}
]]>
< ![CDATA[
Try catch works. Thanks Pierre Hébert.
By the way item.setImage() has the same exception “bug”.
Also the exception told the right Script and line number. But by click in show script the second script after the desired script is shown in script editor.
]]>