Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int io.realm.RealmResults.size()' on a null object reference
at com.subhasishlive.goalDiary.adapters.AdapterGoals.getItemCount(AdapterGoals.java:59)
at com.subhasishlive.goalDiary.widgets.GoalRecyclerView.toggleViews(GoalRecyclerView.java:70)
at com.subhasishlive.goalDiary.widgets.GoalRecyclerView.access$000(GoalRecyclerView.java:21)
at com.subhasishlive.goalDiary.widgets.GoalRecyclerView$1.onChanged(GoalRecyclerView.java:35)
at com.subhasishlive.goalDiary.widgets.GoalRecyclerView.setAdapter(GoalRecyclerView.java:109)
at com.subhasishlive.goalDiary.Activitymain.onCreate(Activitymain.java:81)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
In Adapter class::::::::::::::::::::::::
@Override
public int getItemCount() {
return mReasults.size();
}
this is where the error occurs
In ActivityMain.java,I've instantiated like this:::::::::::::::: mRealm = Realm.getDefaultInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRealm = Realm.getDefaultInstance();
/**
* RealmResults is a special typer of array list.
* which is capable of managing data from realm database...
*/
RealmResults<Goal> results = mRealm.where(Goal.class).findAllAsync();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mEmptyView = (View) findViewById(R.id.empty_goals);
mBtnAdd = (Button) findViewById(R.id.btn_add);
mRecycler = (GoalRecyclerView) findViewById(R.id.rv_goals);
mRecycler.hideIfEmpty(mToolbar);
mRecycler.showIfEmpty(mEmptyView);
// setting up layout manager for mRecycler RecyclerView....
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecycler.setLayoutManager(manager);
// now I have set adapter on recycler view...
// by calling setAdapter method.
// passing new instance of my adapter class as argument.
// and while instanciating the adapter class, passing this present context
// as argument....
mAdapter = new AdapterGoals(this, mResults);
mRecycler.setAdapter(mAdapter);
mBtnAdd.setOnClickListener(mBtnAddListener);
setSupportActionBar(mToolbar);
initBackgroundImage();
}
My Adapter class, in which mReasults is defined ::::::::::::::::::::::: Inside update(), it is initialized to the whatever parameter is passed. Then How can it still be null ???
package com.subhasishlive.goalDiary.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.subhasishlive.goalDiary.R;
import com.subhasishlive.goalDiary.beans.Goal;
import java.util.ArrayList;
import io.realm.RealmResults;
/**
* Created by SubhasishNath on 5/3/2018.
*/
public class AdapterGoals extends RecyclerView.Adapter<AdapterGoals.GoalHolder>{
private LayoutInflater mInflater;
// creating RealmResult array type instance variable,
// that can hold Goal type RealmObjects...
private RealmResults<Goal> mReasults;
public static final String TAG = "SUBHASISH";
public AdapterGoals(Context context,RealmResults<Goal> results){// Inside parameterized constructor,
mInflater = LayoutInflater.from(context);
//mReasults = results;
update(results);
}
// created the public method update,which takes a RealmResults type array...
public void update(RealmResults<Goal> results) {
mReasults = results;
// TODO not updating the list after adding new goal...from video (067 show data inside adapter...)
this.notifyDataSetChanged();
}
// this method returns RecyclerView.ViewHolder
@Override
public GoalHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.row_goals,parent,false);
GoalHolder holder = new GoalHolder(view);
Log.d(TAG, "onCreateViewHolder: ");
return holder;
}
// the returned RecyclerView.ViewHolder from onCreateViewHolder() method is
// passed as parameter in onBindViewHolder() class...
@Override
public void onBindViewHolder(GoalHolder holder, int position) {
Goal goal = mReasults.get(position);
holder.mTextWhat.setText(goal.getWhat());
Log.d(TAG, "onBindViewHolder: "+ position);
}
@Override
public int getItemCount() {
return mReasults.size();
}
// creating custom class
public static class GoalHolder extends RecyclerView.ViewHolder{
TextView mTextWhat;
// parameterized constructor, that takes a View as argument...
public GoalHolder(View itemView) {
super(itemView);
mTextWhat = (TextView) itemView.findViewById(R.id.tv_what);
}
}
}
Any help will be appreciated, thanks in advance :)