I'm trying to set JButton ImageIcon with an image taken from a URL. This is the code I have:
try{
URL imageFile=new URL(picturePath);
carPic=ImageIO.read(imageFile);
picturePane.setIcon(new ImageIcon(carPic));//this line gives the exception
}catch(IOException e){
e.printStackTrace();
}
Where "picturePath" is the image URL.
The URLs I plan to use are just the normal ones obtained with "copy image address" and read as such: "https://www.topgear.com/sites/default/files/544822_0.jpg"
Do I have to do something to the URLs to get them to work right, or is there some other problem with the code itself?
ImageIOto generate aNullPointerExceptionbecause the URL content was invalid - it would generally throw aIOExceptionof some kind instead if it failed to read the URL - my suspicion is that either thepicturePathorpicturePaneare actuallynullinstead - which line generates the NPE?picturePathis a String and I put a line to print it beforehand and that works fine, so I know it's not null. I just ran a test onpicturePaneand it does return null, so I'll try to fix that. Thank you.