Support Topics Documentation Slack YouTube Blog

Retrieving a List of BackendlessUsers

Hey, I’m having some trouble retrieving users from a relation, I need a list of the users that are related to a certain group and I’m having some issues saving the users into a List.

Edit* I’ll put my code here, it’s getting a nullPointerException.

private void findMembers(){
    LoadRelationsQueryBuilder<Map<String,Object>> loadRelationsQueryBuilder;
    loadRelationsQueryBuilder = LoadRelationsQueryBuilder.ofMap();
    loadRelationsQueryBuilder.setRelationName( "users" );

    Backendless.Data.of("Group").loadRelations(groupObjectId, loadRelationsQueryBuilder, new AsyncCallback<List<Map<String, Object>>>() {
        @Override
        public void handleResponse(List<Map<String, Object>> response) {
            for(Map e: response){
                Members mem = new Members();
                mem.setObjectId(e.get("objectId").toString());
                try{
                    mem.setProPic(e.get("propic").toString());
                }catch(NullPointerException exc){
                    Log.d("Exception", exc.toString());
                }
                mem.setName(e.get("name").toString());
                members.add(mem);
            }
            MembersAdapter membersAdapter = new MembersAdapter(getActivity(), members);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(membersAdapter);

        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Log.d("INFO","failed to search");
            Toast.makeText(getContext(), "Error on gathering posts", Toast.LENGTH_SHORT).show();

        }
    });

}

Thank you,
Alex

Hi, Alex.
I’ve tried to recreate the environment and your request. So i had a table Group with relation 1:n to the User. Table User contains several users who are in two different groups.

The minimal working example:

  private static void findMembers(String groupObjectId){

    LoadRelationsQueryBuilder<Map<String,Object>> loadRelationsQueryBuilder;
    loadRelationsQueryBuilder = LoadRelationsQueryBuilder.ofMap();
    loadRelationsQueryBuilder.setRelationName( "users" );

    Backendless.Data.of("Group").loadRelations(groupObjectId, loadRelationsQueryBuilder, new AsyncCallback<List<Map<String, Object>>>() {
      @Override
      public void handleResponse(List<Map<String, Object>> response) {
        System.out.println(response);
      }

      @Override
      public void handleFault(BackendlessFault fault) {
          System.out.println(fault);
      }
    });
  }

then i make call

findMembers( "ABE06FAA-D3E4-6CD1-FF2B-DB62F4282401" );

and all works fine.

Could you, please, check what line of code produce NPE?
What is the value of groupObjectId during the call?