Error when attempt to load image using ImageIO.read-Collection of common programming errors
After searching for similar questions I would like to ask the following question:
I am trying to load a picture from Google Static Maps, but I get this error:Exception in thread "main" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) at MapPicturePanel.main(MapPicturePanel.java:18)
The following is the full code of this class:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MapPicturePanel {
private JScrollPane getContent(BufferedImage image) {
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return new JScrollPane(label);
}
public static void main(String[] args) throws IOException {
String path = "http://maps.googleapis.com/maps/api/staticmap?center=37.426616,-122.176380&zoom=14&size=800x600&scale=2&format=jpg&maptype=hybrid&sensor=false";
URL url = MapPicturePanel.class.getResource(path);
BufferedImage image = ImageIO.read(url);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new MapPicturePanel().getContent(image));
f.setSize(800,800);
f.setLocation(200,200);
f.setVisible(true);
}
}
Originally posted 2013-11-09 23:21:53.