首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试对null对象引用调用虚方法“int io.realm.RealmResults.size()”

尝试对null对象引用调用虚方法“int io.realm.RealmResults.size()”
EN

Stack Overflow用户
提问于 2018-06-09 01:14:30
回答 1查看 845关注 0票数 -1
代码语言:javascript
复制
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)

在Adapter类中:

代码语言:javascript
复制
@Override
public int getItemCount() {
    return mReasults.size();
}

这就是错误发生的地方

在ActivityMain.java中,我像这样实例化: mRealm = Realm.getDefaultInstance();

代码语言:javascript
复制
@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();
    } 

我的适配器类,其中定义了更新:在update()中,它被初始化为传递的任何参数。那么它怎么还是空的呢?

代码语言:javascript
复制
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);
        }
    }



}

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

EN

回答 1

Stack Overflow用户

发布于 2018-06-09 08:53:45

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

代码语言:javascript
复制
@Override
public int getItemCount() {
    Realm mRealm;
    mRealm = Realm.getDefaultInstance();
    RealmResults<Goal> mReasults = mRealm.where(Goal.class).findAllAsync();
    return mReasults.size();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50765549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档