How to resolve java.net.MalformedURLException?

ManJan picture ManJan · Feb 23, 2012 · Viewed 43.5k times · Source

I am getting this error:

java.net.MalformedURLException: Protocol not found[java.lang.StringBuilder] 

When the following line is getting executed:

url = new URL(urlString.toString());

urlString stores the following value:

http://maps.google.com/maps?f=d&hl=en&saddr=25.04202,121.534761&daddr=25.05202,121.554761&ie=UTF8&0&om=0&output=kml

What causes this Exception?

Answer

Snicolas picture Snicolas · Feb 23, 2012

Chances are that you didn't clean after changing from

url = new URL( urlString );

to

url = new URL(urlString.toString());

You should log the value of the parameter passed to the constructor of URL. It's not what you think it should be.

urlString would print a value in the form of java.lang.StringBuilder@ thus throwing the exception if you try to build a url out of that.

But using to String will print the value of the content string built by the stringbuilder.