尝试在空对象引用上调用虚方法'int io.realm.RealmResults.size()'

时间:2018-06-08 17:14:30

标签: android

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)

在适配器类:::::::::::::::::::::::::

@Override
public int getItemCount() {
    return mReasults.size();
}

这是发生错误的地方

在ActivityMain.java中,我实例化了这样:::::::::::::::: 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();
    } 

我的适配器类,其中定义了 mReasults :::::::::::::::::::::::: 在update()内部,它被初始化为传递的任何参数。 然后它怎么还能为空???

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);
        }
    }



}

任何帮助将不胜感激,提前感谢:)

1 个答案:

答案 0 :(得分:0)

我重新分配了mRealm = Realm.getDefaultInstance();从getCount()里面,现在它可以工作:)

@Override
public int getItemCount() {
    Realm mRealm;
    mRealm = Realm.getDefaultInstance();
    RealmResults<Goal> mReasults = mRealm.where(Goal.class).findAllAsync();
    return mReasults.size();
}