Search code examples
javaswingjoptionpaneimageicon

MalformedURLException in java application


i wrote a simple program which is in my book. but i'm getting the MalformedURLException exception.This is my code

import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class ImageGreet{
    public static void main(String []args){
        URL imageLocation = new URL("http://horstmann.com/java4everyone/duke.gif");
        JOptionPane.showMessageDialog(null,"Hello","Title",JOptionPane.PLAIN_MESSAGE,new ImageIcon(imageLocation));
    }
}

but my friend said he got it right with the same code. what's wr9ong with my code? Is it because of the internet connection(I'm using a dial-up connection) I


Solution

  • Since Java has checked exception you have to add throws MalformedURLException to your methods header or you have to write the method logic inside try/catch blocks.