我的AsyncTask中的java.lang.NullPointerException

时间:2013-08-08 15:01:18

标签: java android android-asynctask

我在这段代码中得到了一个N​​PE,我需要很快修复,有人可以帮我一把吗?

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.os.AsyncTask;

public class GetXML extends AsyncTask<String, String, BirdData> {

public GetXML(){

}

/**
 * background
 */
@Override
protected BirdData doInBackground(String... params) {
    String GetWikiID = params[0];
 // All static variables
    String URL = "http://www.bbc.co.uk/nature/life/"+GetWikiID+".rdf";
    // XML node keys

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element
    System.out.println("test3");
    System.out.println(doc.getElementById("dc:description").getTextContent());
    return null;
}

/**
 * on getting result
 */
@Override
protected void onPostExecute(BirdData result) {

}
}

我正在使用此代码来调用它:

System.out.println(WikiID);
new GetXML().execute(WikiID);

,错误是这样的:

    08-08 16:04:17.284: E/AndroidRuntime(17971): FATAL EXCEPTION: AsyncTask #1
08-08 16:04:17.284: E/AndroidRuntime(17971): java.lang.RuntimeException: An error occured while executing doInBackground()
08-08 16:04:17.284: E/AndroidRuntime(17971):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.lang.Thread.run(Thread.java:856)
08-08 16:04:17.284: E/AndroidRuntime(17971): Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 45: http://www.bbc.co.uk/nature/tools/search/Type text here...
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.net.URI.create(URI.java:727)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at com.swift.birdspot.JSONParser.getJSONFromUrl(JSONParser.java:36)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at com.swift.birdspot.GetWikiID.doInBackground(GetWikiID.java:31)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at com.swift.birdspot.GetWikiID.doInBackground(GetWikiID.java:1)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-08 16:04:17.284: E/AndroidRuntime(17971):    ... 5 more

2 个答案:

答案 0 :(得分:1)

错误是以下网址上存在无效字符(空格字符):

http://www.bbc.co.uk/nature/tools/search/Type text here...

您应修复此网址,您的问题将会解决!

答案 1 :(得分:0)

原来我需要做的就是在我的jsonparser类中进行url编码。