-5

java.lang.NullPointerException occuring Log-cat says Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference

Here's my code: I'm trying to get jSon data from a server(MySql) database, then displaying it in a list.

public class AllProducts extends ListActivity {

    private ProgressDialog pDialog;
    JSONParser jParser=new JSONParser();

    ArrayList<HashMap<String,String>> productsList;

    private static String url_all_products = "http://server:host.../view.php";

    private static final String TAG_SUCCESS="success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";

    JSONArray products=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.all_products);

        productsList=new ArrayList<HashMap<String, String>>();

        new LoadAllProducts().execute();

Exception at new LoadAllProducts().execute();

        ListView lv = getListView();



    }

    class LoadAllProducts extends AsyncTask<String,String,String>

Java.lang.NullPointerException Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference occuring at class LoadAllProducts extends AsyncTask<String,String,String>

    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog= new ProgressDialog(AllProducts.this);
            pDialog.setMessage("Loading Products....Wait..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

Exception at pDialog.show(); }

        @Override
        protected String doInBackground(String... params) {


            ContentValues param=new ContentValues();
            JSONObject json= jParser.makeHttpRequest(url_all_products,"GET",param);
            Log.d("All Products: ", json.toString());

java.lang.NullPointerException occuring at Log.d("All Products: ", json.toString());

            try{
                int success=json.getInt(TAG_SUCCESS);
                if(success==1)
                {
                    products = json.getJSONArray(TAG_PRODUCTS);
                    for(int j=0;  j<products.length() ; j++ )
                    {
                        JSONObject c = products.getJSONObject(j);
                        String id=c.getString(TAG_PID);
                        String name=c.getString(TAG_NAME);
                        HashMap<String,String> map=new HashMap<String,String>();
                        map.put(TAG_PID,id);
                        map.put(TAG_NAME,name);
                        productsList.add(map);


                    }

                }
                else {
                    Intent i=new Intent(getApplicationContext(),NewProduct.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }


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

            return null;

        }


    }
}

I have a button in my UI to get data from server and show it in a list, as i tap the button, pDialog shows and then the app goes "not responding" and these exceptions occur

Here is the LogCat

09-03 16:10:40.500  27696-31923/com.example.vashisht.myapplication W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
09-03 16:10:40.500  27696-31923/com.example.vashisht.myapplication W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
09-03 16:10:40.500  27696-31923/com.example.vashisht.myapplication W/System.err﹕ ... 19 more
09-03 16:10:40.500  27696-31923/com.example.vashisht.myapplication E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
09-03 16:10:40.500  27696-31923/com.example.vashisht.myapplication E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
09-03 16:10:40.510  27696-31923/com.example.vashisht.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.vashisht.myapplication, PID: 27696
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
            at com.example.vashisht.myapplication.AllProducts$LoadAllProducts.doInBackground(AllProducts.java:76)
            at com.example.vashisht.myapplication.AllProducts$LoadAllProducts.doInBackground(AllProducts.java:57)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
09-03 16:10:40.980  27696-27696/com.example.vashisht.myapplication E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.vashisht.myapplication.AllProducts has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{10a4b377 V.E..... R......D 0,0-1026,348} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:382)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:298)
            at com.example.vashisht.myapplication.AllProducts$LoadAllProducts.onPreExecute(AllProducts.java:67)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
            at android.os.AsyncTask.execute(AsyncTask.java:535)
            at com.example.vashisht.myapplication.AllProducts.onCreate(AllProducts.java:50)
            at android.app.Activity.performCreate(Activity.java:5958)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
09-03 16:10:44.250  27696-31923/com.example.vashisht.myapplication D/Process﹕ killProcess, pid=27696
09-03 16:10:44.260  27696-31923/com.example.vashisht.myapplication D/Process﹕ com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
iaaphpd13
  • 29
  • 1
  • 2
  • 9
  • 2
    Please post full logcat insted a line – Michele Lacorte Sep 03 '15 at 10:38
  • 2
    if Log.d("All Products: ", json.toString()); throws NullPointerException then your json response is null. Try putting json!=null before your log statement. Let me know if this works. Put this line above your log statement if(json!=null) – Amrut Sep 03 '15 at 10:41
  • @Amrut yes, this worked! The json I'm getting is null – iaaphpd13 Sep 03 '15 at 10:47

1 Answers1

0

this means that the JSONObject json == null. You can check this in code before calling methods on the JSONObject. I guess you dont actually get data when you call the httprequest.

Malte
  • 589
  • 5
  • 24
  • Yes, i think i'm not getting any data from httprequest. – iaaphpd13 Sep 03 '15 at 10:49
  • then debug your code properly and check what your response looks like, and whether you get a response. - Also check on server-side whether the request is handled – Malte Sep 03 '15 at 10:51