#How to catch ImageFetcher exceptions?

1 messages · Page 1 of 1 (latest)

ancient gull
#

I have a set of classes that derive from Node. Most classes have a public static final ImageIcon ICON. I wrote a test to ensure that all nodes have icons.
When it runs I get

Found 56 subclasses of Node
com.marginallyclever.ro3.node.nodes.DHParameter: found
com.marginallyclever.ro3.node.nodes.HingeJoint: found
com.marginallyclever.ro3.node.nodes.LinearJoint: found
com.marginallyclever.ro3.node.nodes.Material: found
com.marginallyclever.ro3.node.nodes.Motor: found
Uncaught error fetching image:
java.lang.NullPointerException: Cannot invoke "java.net.URL.openConnection()" because "this.url" is null
    at java.desktop/sun.awt.image.URLImageSource.getConnection(URLImageSource.java:103)
    at java.desktop/sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
    at java.desktop/sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
    at java.desktop/sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:212)
    at java.desktop/sun.awt.image.ImageFetcher.run(ImageFetcher.java:176)

iconField.get calls ImageFetcher thread, which hangs instead of throwing something I can catch. So... what do? Code to follow in next post.

sinful hemlockBOT
#

<@&987246399047479336> please have a look, thanks.

ancient gull
#
public class TestWhoHasAnIcon {
    @Test
    public void testWhoHasAnIcon() {
        try(ScanResult result = new ClassGraph().enableClassInfo().scan()) {
            var classes = result.getSubclasses(Node.class.getName()).loadClasses();
            System.out.println("Found "+classes.size()+" subclasses of Node");
            for(var cls : classes) {
                System.out.print(cls.getName()+": ");
                try {
                    var iconField = cls.getField("ICON");
                    if(ImageIcon.class.isAssignableFrom(iconField.getType()) &&
                            Modifier.isStatic(iconField.getModifiers())) {
                        System.out.println("found");
                        try {
                            ImageIcon icon = (ImageIcon) iconField.get(null);
                        } catch (NullPointerException e) {
                            System.out.println("found BUT FAILED to load");
                        }
                    }
                }
                catch(NoSuchFieldException e) {
                    System.out.println("NOT found");
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
#

Well at least i know the last "found" is the one with the problem... but I want it to run without hanging, please.

tropic prawn
#

im trying to trace down where this other thread gets created

#

can you share ImageFetcher?

#

looks likey maybe you'll need to hook into doFetch...

grim hound
#

???

ancient gull
#

ImageFetcher is in the default Toolkit, i think?

#

I never instance it myself.

#

oh, it's somewhere in "sun.awt.image"

#

way outside my domain.

#

probably fired up by ImageIcon.

tropic prawn
#

which ImageIcon constructor are you using?

tropic prawn
#

i'm seeing something called a MediaTracker

#
    /**
     * Returns the MediaTracker for the current AppContext, creating a new
     * MediaTracker if necessary.
     */
    private MediaTracker getTracker() {
        Object trackerObj;
        AppContext ac = AppContext.getAppContext();
        // Opt: Only synchronize if trackerObj comes back null?
        // If null, synchronize, re-check for null, and put new tracker
        synchronized(ac) {
            trackerObj = ac.get(TRACKER_KEY);
            if (trackerObj == null) {
                Component comp = new Component() {};
                trackerObj = new MediaTracker(comp);
                ac.put(TRACKER_KEY, trackerObj);
            }
        }
        return (MediaTracker) trackerObj;
    }
#

...maybe this is useful?