0

I try to load Json from URL. By not using AsyncTask class on previous version, It worked well.
However call getJSONFromUrl(currentURL) directly from main Thread (someone told me) is not accepted in 3.2 or higher Android versons.
So, I tried by the following code.
this is my code:

private static String urlTruyenMoiNhat = "myUrl";
private static String currentURL = urlTruyenMoiNhat;
private static JSONObject jSonGetFromCurrentURL = null;

//some stuff....
currentURL = urlTruyenMoiNhat;
GetJsonAsync getJson = new GetJsonAsync();
getJson.execute();
RetreiveListStory(jSonGetFromCurrentURL);

This is my GetJsonAsync class:

private class GetJsonAsync extends AsyncTask <String, Void, String> {

    @Override
    protected void onPreExecute() {
        // Do stuff before the operation
    }

    protected String doInBackground(String... params){
        JSONParser jParser = new JSONParser();
        jSonGetFromCurrentURL = jParser.getJSONFromUrl(currentURL);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // Do stuff after the operation
    }
}

I don't know why it through exception:

11-28 13:30:44.084: E/AndroidRuntime(828): FATAL EXCEPTION: main
11-28 13:30:44.084: E/AndroidRuntime(828): java.lang.RuntimeException: Unable to start activity ComponentInfo{vn.truyencuoihay/vn.truyencuoihay.MainActivity}: java.lang.NullPointerException  

Please help me overcome this problem.
Thanks!

3
  • Please 1) post the full exception stack trace, including the "caused by" parts, if any; 2) read that stack trace and find out which line in your code it points to; 3) mark that line in the code you posted (e.g. by a comment) Commented Nov 28, 2013 at 7:25
  • Post your full stack trace so we can know that at which line it throws null pointer exceptions??? Commented Nov 28, 2013 at 8:15
  • NullPointerException is in the line RetreiveListStory(jSonGetFromCurrentURL);. My problem is solved by moving the line RetreiveListStory(jSonGetFromCurrentURL); into onPostExecute(); Commented Nov 28, 2013 at 9:11

2 Answers 2

1

What actually happens in AsyncTask is when u call execute.. it starts a separate thread.. and all your other commands are excuted in Oncreate() without waiting for the result (in your case jSonGetFromCurrentURL)form AsyncTask .. so u get null pointer exception..

write this line in onPostExecute();

RetreiveListStory(jSonGetFromCurrentURL);

FYI AsyncTask

actually means

AsyncTask <String input, Void progress, String result> 
Sign up to request clarification or add additional context in comments.

1 Comment

NullPointerException is in the line RetreiveListStory(jSonGetFromCurrentURL);. My problem is solved by moving the line RetreiveListStory(jSonGetFromCurrentURL); into onPostExecute(); Thanks for your answer.
0

why are u returning null at this line- jSonGetFromCurrentURL = jParser.getJSONFromUrl(currentURL); return null;

return jSonGetFromCurrentURL.instead of null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.