url.openstream() or urlconnection.getinputstream() raise nullpointerexception on valid URL

混江龙づ霸主 提交于 2019-12-22 01:22:53

问题


For an android application I am trying to change an URL of an XML because of a new server, but the content is the same. I used to do this:

InputSource ins = new InputSource(rssUrl.openStream());

This used to work and for a few days still does, while the old URL is available on on the old server. But when i change the URL of the XML I get an NullPointerException on the row above.

I am sure rssUrl is not null, I printed the value of it an is just the working URL. I validated the XMl and the XML is valid, but this doesn't really matter because the problem is raised before the parsing starts.

One of the things I tried was something like this:

Urlconnection urlConnection= rssUrl.openurlconnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
inputsource ins = new inputsource(in);

but this raised an nullpointerexception as well.

I read some things about downgrading my Java to 6 (currently i am using 7). But i don't really feel like it and can't imagine why it would work.

edit: I tried downgrading to java se 6 but didn't work.

Does anyone know how this error is possible and how to resolve it?

stack:

12-28 22:14:53.635: W/System.err(6949): java.lang.NullPointerException
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpConnection$Address.hashCode(HttpConnection.java:343)
12-28 22:14:53.725: W/System.err(6949):     at java.util.HashMap.get(HashMap.java:298)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:67)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
12-28 22:14:53.735: W/System.err(6949):     at java.net.URL.openStream(URL.java:462)
12-28 22:14:53.735: W/System.err(6949):     at com.hera.ontdekdelft.StartUp$LoadData.doInBackground(StartUp.java:610)
12-28 22:14:53.735: W/System.err(6949):     at com.hera.ontdekdelft.StartUp$LoadData.doInBackground(StartUp.java:1)
12-28 22:14:53.735: W/System.err(6949):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-28 22:14:53.735: W/System.err(6949):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-28 22:14:53.735: W/System.err(6949):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-28 22:14:53.735: W/System.err(6949):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-28 22:14:53.735: W/System.err(6949):     at j.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-28 22:14:53.735: W/System.err(6949):     at java.lang.Thread.run(Thread.java:856)
ava.util

Solution

Underscores in the URL caused android to fail. My solution was editing the url of the XML file so rssUrl.openstream() would work again. problem solved, thanks to Kiruwka


回答1:


According to the trace you provided it seems that uri.getHost() is null, throwing NPE to you from internal implementation. You could verify if it is null by outputting :

Log.d(TAG, "host = " + rssUrl.getHost());

before opening connection. If it is null, fix your url.

Update
I saw several similar issues for cases when host string contains spaces or '_' characters, for example this known bug, or This solution for underscore symbols issue.



来源:https://stackoverflow.com/questions/20817383/url-openstream-or-urlconnection-getinputstream-raise-nullpointerexception-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!