Skip to content
Advertisement

Try/Catch isn’t catching my null Image exception from URL in JavaFX

Okay so, I have a Circle component which I assigned in SceneBuilder. I want to fill this Circle with an ImagePattern object, however I’m running into a weird problem. I’m assigning a lot of images from lots of URL’s in a database and they’re only set when I click them, and some of the links appear to be broken, but for some reason I can’t prevent my program from crashing using try/catch.

Here are examples of working links:

https://a0.muscache.com/im/users/67564/profile_pic/1320663729/original.jpg?aki_policy=profile_x_medium

https://a0.muscache.com/im/users/168920/profile_pic/1279390466/original.jpg?aki_policy=profile_x_medium

https://a0.muscache.com/im/users/502496/profile_pic/1385405597/original.jpg?aki_policy=profile_x_medium

Here is an example of a link which crashes my program:

https://a0.muscache.com/im/pictures/c5b5bc4e-0133-4103-b68e-66f33bbf9e27.jpg?aki_policy=profile_x_medium

As you can see the link leads to a Null directory, and I get this error in my console:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Image must be non-null

So I assumed I could fix this with a simple try/catch, so I did the following:

    try {
        circle.setFill(new ImagePattern(new Image("https://a0.muscache.com/im/pictures/c5b5bc4e-0133-4103-b68e-66f33bbf9e27.jpg?aki_policy=profile_x_medium")));
    }
    catch (Exception e){
        e.printStackTrace();
    }

And nada, it doesn’t catch my error. I’ve tried using the correct IllegalArgumentException catch, I’ve tried ignore the catch, no differences.

I’ve made a mini recreation of my problem below and the error is still prevalent:

public class Test extends Application {

    @Override
    public void start(Stage stage){

BorderPane root = new BorderPane();
        Circle circle = new Circle(50);
        root.setCenter(circle);

        // Works fine (Working link)
        //circle.setFill(new ImagePattern(new Image("https://a0.muscache.com/im/users/67564/profile_pic/1320663729/original.jpg?aki_policy=profile_x_medium")));

        // Dies and cries (Broken link)
        try {
            circle.setFill(new ImagePattern(new Image("https://a0.muscache.com/im/pictures/c5b5bc4e-0133-4103-b68e-66f33bbf9e27.jpg?aki_policy=profile_x_medium")));
        }
        catch (Exception e){
            e.printStackTrace();
        }

        scene = new Scene(root);
        stage.setTitle("test");
        stage.setScene(scene);
        stage.show();

}

}

Just to affirm what my goal is, I just want my program to completely ignore this line if the link is invalid, how would I do this? Thanks.

Also in case there’s any confusion as to why I’m trying to load a dead link, as I said before I’m loading images from a database so I have no idea which links work and which don’t, and I don’t know how to check and researching just seems to tell me to use a try/catch.

Advertisement

Answer

Okay nevermind I found how to fix it, there’s a method in Image for checking if there’s an error:

image.isError()
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement