Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • RecyclerView is not able to reference the TextView

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 625
    Answer it

    I am developing an application that connects to a database and displays the records of each table in a RecyclerView that has CardViews inside, but when I run the application I get the following message in the Android Studio compiler:

    E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.clinicabd, PID: 2301 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.clinicabd.AdapterPaciente.onBindViewHolder(AdapterPaciente.java:57) at com.example.clinicabd.AdapterPaciente.onBindViewHolder(AdapterPaciente.java:18)

    Returning to the application, in the RecyclerView adapter file, which is where these error lines refer me, it appears that the textviews are not correctly related in the project because they are null, I attach the adapter encoding:

    public class AdapterPaciente extends RecyclerView.Adapter<AdapterPaciente.ViewHolder> {
        public class ViewHolder extends RecyclerView.ViewHolder{
            private TextView name;
            private TextView direccion;
            private TextView telCasa;
            private TextView telMovil;
            private TextView nombreEmer;
            private TextView telEmer;
            private TextView observaciones;
            private TextView codigo;
    
            public ViewHolder(@NonNull View itemView){
                super(itemView);
                name = (TextView) itemView.findViewById(R.id.tvNombre);
                direccion = (TextView) itemView.findViewById(R.id.tvDireccion);
                telCasa = (TextView) itemView.findViewById(R.id.tvTelCasa);
                telMovil = (TextView) itemView.findViewById(R.id.tvTelMovil);
                nombreEmer = (TextView) itemView.findViewById(R.id.tvNombreEmer);
                telEmer = (TextView) itemView.findViewById(R.id.tvTelEmer);
                observaciones = (TextView) itemView.findViewById(R.id.tvObservar);
                codigo = (TextView) itemView.findViewById(R.id.tvCode);
            }
        }
    
        public List<elemento_paciente> listapaciente;
    
        public AdapterPaciente(List<elemento_paciente> items) {
            this.listapaciente = items;
        }
    
        @Override
        public AdapterPaciente.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_paciente, parent, false);
    
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull AdapterPaciente.ViewHolder holder, int position) {
            holder.name.setText(listapaciente.get(position).getName());
            holder.direccion.setText(listapaciente.get(position).getDireccion());
            holder.telCasa.setText(listapaciente.get(position).getTelCasa());
            holder.telMovil.setText(listapaciente.get(position).getTelMovil());
            holder.nombreEmer.setText(listapaciente.get(position).getNombreEmer());
            holder.telEmer.setText(listapaciente.get(position).getTelEmer());
            holder.observaciones.setText(listapaciente.get(position).getObservaciones());
            holder.codigo.setText(listapaciente.get(position).getCodigo());
        }
    
        @Override
        public int getItemCount(){
            return listapaciente.size();
        }
    }
    

    Line 57 that gives me an error is the following:

    holder.name.setText(listapaciente.get(position).getName());
    

    And therefore the lines that follow where the other fields of the table are captured will have the same error.

    The elemento_paciente.java file, which is where the getName() method and the other methods are initialized, is as follows:

    public class elemento_paciente {
        private String name;
        private String direccion;
        private int telCasa;
        private int telMovil;
        private String nombreEmer;
        private int telEmer;
        private String observaciones;
        private int codigo;
    
        public elemento_paciente() {
    
        }
    
        public elemento_paciente(String name, String direccion, int telCasa, int telMovil, String nombreEmer, int telEmer, String observaciones, int codigo) {
            this.name = name;
            this.direccion = direccion;
            this.telCasa = telCasa;
            this.telMovil = telMovil;
            this.nombreEmer = nombreEmer;
            this.telEmer = telEmer;
            this.observaciones = observaciones;
            this.codigo = codigo;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDireccion() {
            return direccion;
        }
    
        public void setDireccion(String direccion) {
            this.direccion = direccion;
        }
    
        public int getTelCasa() {
            return telCasa;
        }
    
        public void setTelCasa(int telCasa) {
            this.telCasa = telCasa;
        }
    
        public int getTelMovil() {
            return telMovil;
        }
    
        public void setTelMovil(int telMovil) {
            this.telMovil = telMovil;
        }
    
        public String getNombreEmer() {
            return nombreEmer;
        }
    
        public void setNombreEmer(String nombreEmer) {
            this.nombreEmer = nombreEmer;
        }
    
        public int getTelEmer() {
            return telEmer;
        }
    
        public void setTelEmer(int telEmer) {
            this.telEmer = telEmer;
        }
    
        public String getObservaciones() {
            return observaciones;
        }
    
        public void setObservaciones(String observaciones) {
            this.observaciones = observaciones;
        }
    
        public int getCodigo() {
            return codigo;
        }
    
        public void setCodigo(int codigo) {
            this.codigo = codigo;
        }
    }
    

    Finally, the pacientes.java file is where the database is connected and the data is displayed in the recyclerView, and the adapter is called.

    public class pacientes extends AppCompatActivity {
    
        private RecyclerView rvLista;
        private AdapterPaciente adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pacientes);
    
            rvLista = (RecyclerView) findViewById(R.id.rvListaPacientes);
            rvLista.setLayoutManager(new LinearLayoutManager(this));
    
            adapter=new AdapterPaciente(obtainPaciente());
            rvLista.setAdapter(adapter);
        }
    
        public List<elemento_paciente> obtainPaciente(){
            ConexionBD cn=new ConexionBD();
            List<elemento_paciente> paciente= new ArrayList<>();
    
            try{
                Statement leerDatos=cn.Conectar().createStatement();
                ResultSet rs=leerDatos.executeQuery("select * from Paciente");
    
                while(rs.next()){
                    paciente.add(new elemento_paciente(rs.getString("nombre"), rs.getString("direccion"), rs.getInt("tel_casa"),
                            rs.getInt("tel_movil"), rs.getString("nombre_emergencia"), rs.getInt("tel_emergencia"), rs.getString("observaciones"),
                            rs.getInt("cod_paciente")));
                }
            }catch (SQLException e){
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
            return paciente;
        }
    
    }
    

    I hope you can help me to fix this issue with the RecyclerView or if for my case it is better to use a listview.

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: