Null object reference when listing data with FirebaseRecyclerAdapter

0

I'm trying to popular a RecyclerView with the FirebaseRecyclerAdapter. When the onBindViewHolder method is called to populate the layout through the ViewHolder class, the error is triggered:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: br.com.coop.ascenderideias.maissaude, PID: 19982
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at br.com.coop.ascenderideias.maissaude.viewHolder.MyViewHolder.bind(MyViewHolder.java:19)
                  at br.com.coop.ascenderideias.maissaude.MainActivity$3.onBindViewHolder(MainActivity.java:93)
                  at br.com.coop.ascenderideias.maissaude.MainActivity$3.onBindViewHolder(MainActivity.java:84)
                  at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
                  at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
                  at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                  at android.view.Choreographer.doFrame(Choreographer.java:603)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                  at android.os.Handler.handleCallback(Handler.java:746)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5443)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

I'm not sure how to solve it. Previously I loaded the data from the firebase to an arraylist, but even then the objects are only listed after the second boot, after installing the application. From what I understand, the data is not being returned, how do I solve it?

Model:

public class Instituicao {
    private String nome;

    public Instituicao() {
    }

    public Instituicao(String nome) {
        this.nome = nome;
    }
    ...
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("Nome", getNome());
        return result;
    }

    @Override
    public String toString() {
        return "Instituicao{" + "Nome='" + nome +'}';   
    }
}

ViewHolder

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import br.com.coop.ascenderideias.maissaude.Dominio.Instituicao;
import br.com.coop.ascenderideias.maissaude.R;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView titulo;
    public MyViewHolder(View itemView) {
        super(itemView);
        titulo = (TextView) itemView.findViewById(R.id.edt_label_nome);
    }

    public void bind(Instituicao model) {
        titulo.setText(model.getNome().toString());
    }
}

Activity

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import br.com.coop.ascenderideias.maissaude.Dominio.Instituicao;
import br.com.coop.ascenderideias.maissaude.viewHolder.MyViewHolder;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    protected static Query instQuery =
            FirebaseDatabase.getInstance().getReference().child("INSTITUICAO").limitToLast(50);

    @BindView(R.id.lista_recyclerview)
    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        mRecyclerView = (RecyclerView) findViewById(R.id.lista_recyclerview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        FloatingActionButton fab = findViewById(R.id.floatingActionButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,CadastroInstituicaoActivity.class));
            }
        });

    }
    @Override
    protected void onStart() {
        super.onStart();
        attachRecyclerViewAdapter();
    }

    @Override
    protected void onStop() {
        super.onStop();

    }
    private void attachRecyclerViewAdapter() {
        final RecyclerView.Adapter adapter = newAdapter();

        // Scroll to bottom on new messages
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                mRecyclerView.smoothScrollToPosition(adapter.getItemCount());
            }
        });

        mRecyclerView.setAdapter(adapter);
    }

    protected RecyclerView.Adapter newAdapter() {
        FirebaseRecyclerOptions<Instituicao> options =
                new FirebaseRecyclerOptions.Builder<Instituicao>()
                        .setQuery(instQuery, Instituicao.class)
                        .setLifecycleOwner(this)
                        .build();

        return new FirebaseRecyclerAdapter<Instituicao, MyViewHolder>(options) {
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_lista_instituicao, parent, false));
            }

            @Override
            protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Instituicao model) {
                holder.bind(model);
            }

            @Override
            public void onDataChanged() {
                // If there are no chat messages, show a view that invites the user to add a message.

            }
        };
    }

}
    
asked by anonymous 31.05.2018 / 15:31

1 answer

0

I do not see your class Listaded, but I suppose the line Listadedados.getDados().get(position) is returning null.

Instead of passing this to your bind method, pass the receiving institution on onBindViewHolder :

    @Override
    protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Instituicao model) {
        holder.bind(model);
    }
    
31.05.2018 / 21:56