add to a frame an image from a URL (WeatherStack API)

Viewed 277

my problem is that I'm using weatherstack API for the weather and it gives me in the JSON (String) a link to a picture(weather_description). so when I put the link in the browser it works and when i try it in my code it doesn't work. my code worked with many other links but with this one, it doesn't. (can someone try to add this picture in a frame and share with me the code?)

URL::  "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png"

here is my code:

public class NewClass {

    public static void main(String[] args) throws MalformedURLException, IOException {
        Image image = null;
        
            URL url = new URL("https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png");
            image= ImageIO.read(url);         
        System.out.println(image);
        JFrame frame = new JFrame();

        JLabel lblimage = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(lblimage, BorderLayout.CENTER);
        frame.setSize(300, 400);
        frame.setVisible(true);
        
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(lblimage);
        frame.add(mainPanel);
        frame.setVisible(true);
    }

}

here is the error I get:

null
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.awt.Image.getProperty(String, java.awt.image.ImageObserver)" because "image" is null
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:240)
at com.mycompany.mavenproject7.NewClass.main(NewClass.java:37)

variable image is null

I would like to understand why it doesn't want to work and what should I do to make it work.

0 Answers
Related