0

I'm trying to call a method in an android application when I press a button but the application crushes when the method is called.

          BtnName.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            get_name_value();
        }
    }); 

public void get_name_value(){

    int success;
    String name = "paul";
    try {
        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("name", name));

        // getting User details by making HTTP request
        // Note that User details url will use GET request
        JSONObject json = jsonParser.makeHttpRequest(
                url_product_detials, "GET", params1);

        // check your log for json response
        Log.d("Single User Details", json.toString());

        // json success tag
        success = json.getInt(TAG_SUCCESS);
        if (success == 1) {
            // successfully received User details
            JSONArray UserObj = json
                    .getJSONArray(TAG_PRODUCT); // JSON Array

            // get first User object from JSON Array
            final JSONObject product = UserObj.getJSONObject(0);
            runOnUiThread(new Runnable() {
                public void run() {
                    // User with this pid found
                    // Edit Text
                        txtName = (EditText) findViewById(R.id.accid);
                    // display User data in EditText
                    try {
                        txtName.setText(product.getString(TAG_VALUE));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });

        }else{
            // User with pid not found
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} 

the following are some of the errors showing.

01-04 09:11:59.665: E/AndroidRuntime(579): FATAL EXCEPTION: main
01-04 09:11:59.665: E/AndroidRuntime(579): java.lang.NullPointerException
01-04 09:11:59.665: E/AndroidRuntime(579):  at com.pule.ConCurrency.Concurency_main$5.run(Concurency_main.java:320)
01-04 09:11:59.665: E/AndroidRuntime(579):  at android.app.Activity.runOnUiThread(Activity.java:3717)
01-04 09:11:59.665: E/AndroidRuntime(579):  at com.pule.ConCurrency.Concurency_main.get_name_value(Concurency_main.java:313)
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • Can you post the whole logcat? – Uma Kanth Jan 05 '16 at 09:53
  • `NullPointerException`s are one of the more commonly encountered exceptions, but also (usually) quite straightforward to fix. [This post will help you with debugging](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – PPartisan Jan 05 '16 at 09:54

1 Answers1

0

First problem is that you try to do network operation on UI thread. You should put this part:

    // getting User details by making HTTP request
    // Note that User details url will use GET request
    JSONObject json = jsonParser.makeHttpRequest(
            url_product_detials, "GET", params1);

inside AsyncTask.doInBackground, and the later code which updates your UI inside AsyncTask.onPostExecute.

Here is a good tutorial using makeHttpRequest for you:

http://techlovejump.com/android-json-parser-from-url/

as you can see this method is executed from inside of AsyncTask.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • btw. from your code I suppose you should be fine if get_name_value(); would get executed from inside of AsyncTask – marcinj Jan 05 '16 at 09:57