Search code examples
javaexceptionmalformedurlexception

MalformedURLException


This is a method to get a page source code turned into a string array and return that array, but for some reason I keep getting exception errors.

When I try to use this class it returns me java.net.MalformedURLException: no protocol

I have the exceptions handled, I don't get it. Can someone please explain to me why I keep getting this error?

 public static String[] Connect(String A) throws IOException,
    MalformedURLException {
    ArrayList<String> myList = new ArrayList<String>();
    URL url = new URL("A");
    URLConnection spoof = url.openConnection();


    spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
    BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
    String strLine = "";

    while ((strLine = in.readLine()) != null){
        myList.add(strLine);
        System.out.println(strLine);
    }
    String[] arr = myList.toArray(new String[myList.size()]);
    return arr;

}

Solution

  • You are passing string "A" as parameter to the URL constructor new URL("A");. "A" is not a valid URL. I think you intended to pass it variable A, right? Remove the " ".