Null Pointer Exception on related object reference

I an currently using this; compile ‘com.backendless:backendless:3.+’ in my gradle file…I used a working code in a recent app which worked very well. I tried to apply the same code in another app an it is giving me errors.

I am suspecting that my sdk might have updated automatically which might be giving me this error.

BackendlessDataQuery dataQuery = new BackendlessDataQuery();
QueryOptions queryOptions = new QueryOptions();
queryOptions.addRelated(“relatedEvents”);
//queryOptions.addSortByOption( “created ASC” );
dataQuery.setQueryOptions(queryOptions);

        Backendless.Data.of(DailySchedule.class).find(dataQuery, new AsyncCallback<BackendlessCollection<DailySchedule>>() {
            @Override
            public void handleResponse(BackendlessCollection<DailySchedule> response) {
             
                RealmResults<DailySchedule> results = realm.where(DailySchedule.class).findAll();
                realm.beginTransaction();
                //clear all existing data of dailySchedule in realm database
                if(results.size()>0)
                    results.deleteAllFromRealm();
                for (int i = 0; i < response.getCurrentPage().size(); i++) {


                    DailySchedule schedule = realm.createObject(DailySchedule.class);
                    // min.setObjectId(response.getData().get(i).getObjectId());
                    schedule.setDayOfTheWeek(response.getData().get(i).getDayOfTheWeek());
                    schedule.setAllEvents(EventsToRealmList(realm,response.getData().get(i).getAllEvents()));


                    System.out.println("Day of the Week  " + response.getData().get(i).getDayOfTheWeek());
                    System.out.println("No. of Related Event for the day = " + response.getData().get(i).getAllEvents().size());


                }
                realm.commitTransaction();
                // mTeaserPostAdapter.notifyDataSetChanged();
                if(response.getCurrentPage().size()==0){
                    snackToast("No new schedule found");
                }
            }


            @Override
            public void handleFault(BackendlessFault fault) {


            }
        });
    }

The error i am getting is that, there is a NPE on a null object reference in this line: response.getData().get(i).getAllEvents().size().

AllEvents table is not empty in my online database but am still getting this error. What might be the issue.

Hi,

Try set relationDepth 1 in your data query.

Regards,

Denys

Hi Denys.

I applied the relationDepth but I still get a null object reference error

Try fetching this object from backendless using the objectId. It fixed my problem.