0
    subMap = new HashMap<String, String>();
    cursor = sdB.query(DataBase.TB, cols, null, null, null, null, null);
    cursor.moveToFirst();
    limit = 1;
    while(cursor.moveToNext()){
        limit +=1;
    }
    limit *= 2;
    cursor.moveToFirst();
    do{
        for(i=i+1; i<limit; i++){
            if(i%2!=0){
                subCode = cursor.getString(cursor.getColumnIndex(DataBase.C_CODE));
                subMap.put(TAG_SUB_CODE, subCode);
                Log.i("Alert", subCode);

                subjects.add(subMap);
                break;
            }
            else{
                subName = cursor.getString(cursor.getColumnIndex(DataBase.SUB_TITLE));
                subMap.put(TAG_SUB_NAME, subName);
                if(subMap.containsKey(TAG_SUB_NAME))
                    Log.i("Alert", "Yes");
                if(subMap.containsValue(subName))
                    Log.i("Alert", "Again Yes");
                Log.i("Alert", TAG_SUB_NAME);
                Log.i("Alert", subName);
                subjects.add(subMap);
            }
        }
    }while(cursor.moveToNext());

    cursor.close();

Gives me an NPE on subjects.add(subMap)in both if and else statement. First else is executed then if. But program stops at first encounter with it in else block. Can Anyone please help me. I checked the existance of value and key in hashmap and it returned me true (yes and again yes ) in my log cat but still the subjects.add(subMap) returns an NPE.

Thank You

  • Where and how you initialize `subjects` ? Because if you get an NPE at this line: `subjects.add(subMap)` then your subjects == null – donfuxx Mar 19 '14 at 21:33
  • you don't show the declaration and definition of `subjects`. How is anyone supposed to know what's wrong with it ? – njzk2 Mar 19 '14 at 21:44
  • Ok...I got it now..Thanks a lot...Thank you very much. Actually i forgot to add subjects = new Arraylist>(); After adding it works. – user3290632 Mar 19 '14 at 21:45

2 Answers2

0

You need to initialize your list subjects. For example like this:

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

If you don't do that then at this line:

subjects.add(subMap);

an NPE will be thrown.

donfuxx
  • 11,277
  • 6
  • 44
  • 76
0

Most likely subjects is null. Have you initialized it like this:

ArrayList<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86