0

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

Community
  • 1
  • 1
Jasper
  • 2,389
  • 4
  • 25
  • 40

1 Answers1

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.

Community
  • 1
  • 1
kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • First part you were right, had removed it in the code and tried to duplicate it here and made a mistake. I tried the rssUrl.getHost() but it just gives back the host, not null. Should the host contain http:// ? I get the part of the url before the backslash, without http:// – Jasper Dec 28 '13 at 22:11
  • could you please give your host string ? – kiruwka Dec 28 '13 at 22:22
  • @Jasper. The problem is with underscore symbol in host string. You will have to change that, I am afraid. See update to my answer – kiruwka Dec 28 '13 at 22:33
  • I'll try it, let you know. Thanks in advance! Had no idea that could bring trouble... – Jasper Dec 28 '13 at 22:36
  • I imagine it might be already fixed on newer android platforms. Which android version you are running your code on ? – kiruwka Dec 28 '13 at 22:44
  • my current testing device is running android 4.0.3 – Jasper Dec 28 '13 at 22:50
  • too bad, that means that bug is not fixed on android at all. So, you have to change your hostname to eliminate underscores, I don't see other way. – kiruwka Dec 28 '13 at 22:55
  • Hello, i am getting one issue. in my case connection.getResponseCode() returns 200 and connection.getResponseMessage() returns OK. but when i am trying to use BufferedReader in1 = new BufferedReader(new InputStreamReader(connection.getInputStream())); responseString1 = in1.readLine(); in1.readLine() always returning null – Ajay Jul 23 '14 at 09:43