0

I am parsing json from url, the below code is simplified one which doesn't work. I am getting a NullPonterException at JSONObject json=jsonParser.getJSONFromUrl(GETSCHOOL_URL);.I found many people have searched for these particular problem, but I couldn't find any solution for my case.

public class SearchActivity extends Activity {
    AutoCompleteTextView act;

    JSONParser jsonParser;
    private static final String TAG_SUCCESS = "success";
    private static final String GETSCHOOL_URL = "http://sample.com/json";
    private static final String TAG_STATUS = "status";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Button b= (Button) findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new GetSchools().execute();
            }
        });

    }

    class GetSchools extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.d("request!", "starting");
        }

        @Override
        protected String doInBackground(String... args) {
            String success;

            try {
                Log.d("request!", "starting");
                JSONObject json = jsonParser.getJSONFromUrl(
                        GETSCHOOL_URL);
                Log.d("data", json.toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {

        }
    }

}

I think its silly mistake that I've done somewhere in program

1 Answer 1

2

You're never initializing jsonParser in the code you pasted. It needs to be initialized before your asyncTask is called.

Sign up to request clarification or add additional context in comments.

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.