0

I am trying to build an app with master and detail layout using fragments and data is coming from from parsing JSOnArray by consuming webservice using Asynctask.

No matter what fixes I apply as suggested for similar issues over various forums, I always see this error from logcat:

FATAL EXCEPTION: main
    Process: com.example.masterdetail, PID: 26639
    java.lang.NullPointerException: println needs a message
            at android.util.Log.println_native(Native Method)
            at android.util.Log.d(Log.java:139)
            at com.example.masterdetail.ItemsListFragment.populateResult(ItemsListFragment.java:108) //this is the line where I am building an arraylist in ItemsListFragment.java
            at com.example.masterdetail.AsyncRequest.onPostExecute(AsyncRequest.java:147)
            at com.example.masterdetail.AsyncRequest.onPostExecute(AsyncRequest.java:23)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

================== ItemsListFragment.java:

public void populateResult(String response) {
        /*TextView resultView = (TextView)getActivity().findViewById(R.id.textUrlContent);
        resultView.setText(response);*/
        try {
            ListView lvItems = (ListView) getActivity().findViewById(R.id.lvItems);
            ArrayList<Item> products = new ArrayList<Item>();
            JSONObject jsonObject = new JSONObject(response);
            JSONObject responseObj = jsonObject.getJSONObject("response");
            JSONArray jsonDataProducts = responseObj.getJSONArray("products");

            for (int i = 0; i < jsonDataProducts.length(); i++) {
                JSONObject oneObject = jsonDataProducts.getJSONObject(i);
                Item dataProduct = new Item();
                dataProduct.setTitle(oneObject.getString("title"));
                dataProduct.setBody(oneObject.getString("id"));
                products.add(dataProduct); //***Line: 108 from above error msg ************
            }

            adapterItems = new ArrayAdapter<Item>(getActivity(),
                    android.R.layout.simple_list_item_activated_1, products);
            lvItems.setAdapter(adapterItems);
            lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View item, int position,
                                        long rowId) {
                    // Retrieve item based on position
                    Item i = adapterItems.getItem(position);
                    // Fire selected event for item
                    listener.onItemSelected(i);
                }
            });
        } catch (Exception e) {
           Log.d("ReadRdaJSONFeedTask", e.getLocalizedMessage());
        }
    }
sunskin
  • 1,620
  • 3
  • 25
  • 49

1 Answers1

4
Log.d("ReadRdaJSONFeedTask", e.getLocalizedMessage());,

The e.getlocalizedMessage() is returning null. Check before calling.

Log.d("ReadRdaJSONFeedTask", e.getLocalizedMessage() == null ? "" : e.getLocalizedMessage());
Submersed
  • 8,810
  • 2
  • 30
  • 38
  • Thanks for your answer. I tried that but still no luck. This issue has been annoying :( – sunskin Jan 21 '15 at 23:36
  • 1
    Why not just e.printStackTrace() – Submersed Jan 21 '15 at 23:41
  • e.printStackTrace() in place of e.getLocalizedMessage()? – sunskin Jan 21 '15 at 23:44
  • thank you! I was looking at the older messages in the log cat. I tried to clear all by right clicking on it but looks like it never cleared it and always loaded the old ones. So, I just restarted android studio and tried the one you mentioned in the answer it worked! – sunskin Jan 21 '15 at 23:56