0

I have to update an expandableList from server, I am using asyncTask to download and fill new values in local database, everything is done in doInBackground but after its done I tried to refresh my expandableList in onPostExecute but I am getting a nullPointer on the very statement which runs fine before loading new data. All I am doing is calling the same method which instializes expandableList in the first place..

The method which intializes list for the first time:

public void initializeExpandableList() {
        // TODO Auto-generated method stub
        getValuesForList();
        setupList();
        setupClickListenersOnExpandList();

    }

And the code for getValuesForList() and setUpList() is:

public void getValuesForList() {

    // get which list_id are present in subtopic table

    DataBaseHelper myDbHelper = new DataBaseHelper(context);
    ArrayList<String> ids = myDbHelper.getListIdsFromSubTopic();

    ArrayList<String> typeList = myDbHelper.getTypeListFromDB(ids);
    // typeList = myDbHelper.getTypeListFromDB();
    Log.v("typelist", typeList + "");
    arrGroupelements = typeList.toArray(new String[typeList.size()]);

    Log.v("arrGrp", arrGroupelements.toString() + "");

    ArrayList<ArrayList<String>> subtopics = myDbHelper.getChildForGroup();

    arrChildelements = new String[subtopics.size()][];
    for (int i = 0; i < subtopics.size(); i++) {
        ArrayList<String> row = subtopics.get(i);
        arrChildelements[i] = row.toArray(new String[row.size()]);
    }
    Log.e("arrChildElements", arrChildelements.toString());

}

And for setUpList() method:

public void setupList() {
            expList.setAdapter(adap);
            // adap.notifyDataSetChanged();
            ((BaseAdapter) expList.getAdapter()).notifyDataSetChanged();
        }

After inserting data in doInBackground(), inside onPostExecute I am calling a method refresh() which again makes a call to initializeExpandableList() where I am getting null pointer.

private void refresh() {
        // TODO Auto-generated method stub
initializeExpandableList();
    }

Instead of initializeExpandableLIst, also tried

((BaseAdapter) expList.getAdapter()).notifyDataSetChanged();

and also tried restarting the activity:

Intent intent = getIntent();
         finish();
         startActivity(intent);

again getting null pointer in last line startActivity(intent);

What am I missing here??

Ayush Goyal
  • 2,079
  • 8
  • 32
  • 47
  • I can suggest you to reload the activity on refresh click. That might solve your prob – kittu88 Nov 22 '12 at 06:17
  • how exactly do I do that? I tried finishing and starting activity but that doesn't resolve anything. – Ayush Goyal Nov 22 '12 at 06:20
  • finish(); startActivity(getIntent()); – kittu88 Nov 22 '12 at 06:21
  • check this out http://stackoverflow.com/questions/3053761/reload-activity-in-android – kittu88 Nov 22 '12 at 06:22
  • already tried, NullPointer at startActivity(getIntent()); – Ayush Goyal Nov 22 '12 at 06:22
  • please finish the activity once. – kittu88 Nov 22 '12 at 06:23
  • Have you passed the context and the expandable list to the AsynTask ? in AsyncTask use Activity activity = (Activity) context; activity.startActivity(); – Amt87 Nov 22 '12 at 06:27
  • passed "getApplicationContext()" in context and I'm calling refresh() method that exists in my activity and proceeding from there. – Ayush Goyal Nov 22 '12 at 06:28
  • 2
    Merely posting code and saying there's a NPE somewhere won't help. You need to pinpoint where it occurs. Look for `caused by` in log cat, find the line number of the last executed method on stack trace, set up a break point there. Debug, and examine what variable in scope is null. – S.D. Nov 22 '12 at 06:31
  • @Singularity its being caused by everything called in refresh(), be it notifydataSet, startActvity, initializeExpandableList() – Ayush Goyal Nov 22 '12 at 06:34
  • What methods are you calling in `doInBackground()` ? Does any of these methods accesses ListView ? – S.D. Nov 22 '12 at 06:38
  • not at all, just downloading json data and inserting in local database.. – Ayush Goyal Nov 22 '12 at 06:43
  • can you paste error log? it will be more clear then. – dd619 Nov 22 '12 at 07:18
  • If refresh() and initializeExpandableList() are in the same scope (activity), then initializeExpandableList() itself can't throw a NPE. Post your whole (slimmed down to what matters) activity and ASyncTask.. + line where the NPE is thrown in THAT code – NickL Nov 22 '12 at 08:12
  • Also, you should not pass getApplicationContext() to the ASyncTask. This is the context for the entire application, not the activity. You should pass 'this', the activity, to the ASyncTask. So probably you are using Context as a parameter of the ASyncTask, cast it to the Activity type and call the refresh method. If it is not the right context, calling the refresh() method itself will cause a NPE. Posting the ASyncTask + NPE line location would make this clear. – NickL Nov 22 '12 at 08:15

0 Answers0