0

It shows that the problem is here:

@Override
public int getChildrenCount(int groupPosition) {
    return hashMap.get(groupPosition).size();
}

I tried to print the map size and it does. It has only two keys: "Upcoming Events" and "Past Events". I checked using the containsKey() and the result was true. But somehow I still can't get the size of the List object contained as a value.

It may help to know that I did not create another class file for this and all the code is bundled up in the same class file.

Here's my code:

class CustomEventListAdapter extends BaseExpandableListAdapter{
    HashMap<String, List<Event>> hashMap;
    Context con;
    CustomEventListAdapter(Context context, HashMap<String, List<Event>> map){
        hashMap = map;
        con = context;
    }

    List<String> groupTitle = Arrays.asList("Upcoming Events","Past Events");

    @Override
    public int getGroupCount() {
        return groupTitle.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return hashMap.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupTitle.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return hashMap.get(groupTitle.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.groupHeader);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(groupTitle.get(groupPosition));
        return convertView;        }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_item, null);
        }
        TextView eventName = (TextView) convertView.findViewById(R.id.eventName);
        TextView eventDate = (TextView) convertView.findViewById(R.id.eventDate);
        TextView eventDesc = (TextView) convertView.findViewById(R.id.eventDesc);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.eventPic);

        eventName.setText(hashMap.get(groupPosition).get(childPosition).getName());
        eventDate.setText(hashMap.get(groupPosition).get(childPosition).getDate());
        eventDesc.setText(hashMap.get(groupPosition).get(childPosition).getDetails());

        return convertView;        }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

This is the onCreate() :

   expandableListView = (ExpandableListView) findViewById(R.id.eventList);
    hashMap  = new HashMap<>();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference reference = database.getReference().child("Events");
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<Event> eventList = new ArrayList<>();
            //List<Event> upcoming = new ArrayList<>();
            //List<Event> past = new ArrayList<>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                Event event = new Event();
                event.name = String.valueOf(postSnapshot.child("Name").getValue());
                event.details = String.valueOf(postSnapshot.child("Details").getValue());
                event.add = String.valueOf(postSnapshot.child("PhotoAdd").getValue());
                event.date = String.valueOf(postSnapshot.child("Date").getValue());

                eventList.add(event);
                Toast.makeText(EventsActivity.this, "longDate = " + event.getLongDate() , Toast.LENGTH_SHORT).show();


            }
            long current = Calendar.getInstance().getTimeInMillis();
            Collections.sort(eventList, new Sortbyroll());
            int i=0;
            for( i=0; i< eventList.size(); i++){
                if(eventList.get(i).getLongDate() < current){
                    break;
                }
            }
            hashMap.put("Upcoming Events", eventList.subList(0,i));
            hashMap.put("Past Events", eventList.subList(i,eventList.size()));

            ExpandableListAdapter listAdapter = new CustomEventListAdapter(getBaseContext(),hashMap);
            expandableListView.setAdapter(listAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
Aditya Nigam
  • 539
  • 2
  • 7
  • 18
  • please provide the code where you create the `HashMap>` map – Lino Jul 15 '18 at 06:32
  • @Lino please have a look again. – Aditya Nigam Jul 15 '18 at 06:42
  • Inclined as closed as a duplicate for https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – lexicore Jul 15 '18 at 06:45
  • @lexicore No sir, I'm not confused about what a NullPointerException is. As you can see I have the value in the map variable and I tested it and printed out the sub list before putting it into the map. – Aditya Nigam Jul 15 '18 at 06:51
  • @AdityaNigam The "duplicate" also provides a lot of guidance on how to diagnose and debug NPEs. – lexicore Jul 15 '18 at 07:00

1 Answers1

0

Maybe the code you put here is not complete because I don't found something like "hashMap.put(groupPosition, ....)". I mean that you trying to GET the content of that Group but there isn't any piece of code where you SET it. The only two "put()" methods are called for special groups called "upcoming events" and "past events".....so the "hashMap" can contain only those two values.

emandt
  • 1,903
  • 2
  • 10
  • 15
  • I realized that just a little while ago and as it happens the list isn't being added to the map. The subList() isn't working for me. Could you suggest me some way to do that? – Aditya Nigam Jul 15 '18 at 07:41
  • You're reading an HashMap key that you NEVER set, so the incriminated code ("hashMap.get(groupPosition)") cannot retrieve a "groupPosition" key if the only two keys you set were "Upcoming Events" and "Past Events". So you have to decide if you have forgotten to create those GroupIDs to be retrieved or if you're retrieving these two keys using a wrong key (the groupPosition Integer key that you NEVER set in the hashMap) – emandt Jul 15 '18 at 07:54
  • I worked up a method where I manually add all the values to the map and it does retain them when checking them in the adapter class. I would like for you to explain the last bit a little more clearly because error is still there. – Aditya Nigam Jul 15 '18 at 12:29
  • Explain more? Mhmmm.... You have to execute an "hashMap.put(INTEGER, value)" before use "hashMap.get(groupPosition)", but the only two "put()" are relative to Strings, so you cannot execute "hashMap.get(groupPosition)" but only "hashMap.get("Upcoming Events")" or" hashMap.get("Past Events")" because there are present only those two in the HashMap. – emandt Jul 15 '18 at 15:01
  • Thanks. Explicitly defining the keys has worked. However, if you have any suggestions about how I could've done it better I would appreciate them. – Aditya Nigam Jul 16 '18 at 05:33