0

I was creating a contact like app with TabLayout and RecyclerView, but am getting null object reference somewhere in my code, i just cant figure out where to patch things, Below is the file where am getting error:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context context;
private List<Contact> data;

RecyclerViewAdapter(Context context, List<Contact> data) {
    this.context = context;
    this.data = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    view = LayoutInflater.from(context).inflate(R.layout.contact_fragment,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    //logcat error pointing below line
    holder.name_tv.setText(data.get(position).getName());
    holder.phone_tv.setText(data.get(position).getPhone());
    holder.photo_iv.setImageResource(data.get(position).getPhoto());
}

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

static class MyViewHolder extends RecyclerView.ViewHolder{

    private TextView name_tv;
    private TextView phone_tv;
    private ImageView photo_iv;

    MyViewHolder(View itemView) {
        super(itemView);

        name_tv = itemView.findViewById(R.id.name_contact);
        phone_tv = itemView.findViewById(R.id.contact_number);
        photo_iv = itemView.findViewById(R.id.image_contact);

    }
   }

  }

And below is whole debug log:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.vijay.recyclerview, PID: 2059
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at com.example.vijay.recyclerview.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:32)
                  at com.example.vijay.recyclerview.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:13)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                  at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3172)
                  at android.view.View.layout(View.java:17995)
                  at android.view.ViewGroup.layout(ViewGroup.java:5817)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2759)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2460)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1528)
                at android.view.ViewRootImpl$TraversalRunnable.
I/Process: Sending signal. PID: 2059 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 
'socket'

Here is contact_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/backgroundFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_contact_frag"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>
</LinearLayout>

And item_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
 <ImageView
android:id="@+id/image_contact"
android:src="@drawable/ic_child_care"
android:layout_width="56dp"
android:layout_height="56dp" />
<LinearLayout
    android:orientation="vertical"
    android:layout_marginLeft="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:textStyle="bold"
        android:text="Contact Name"
        android:id="@+id/name_contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/contact_number"
        android:text="Phone Number Here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
    android:gravity="end"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_call_black_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
 </LinearLayout>

Tell me if i have to paste few more files here. I always get these type of exceptions, if you guys help me to find with this one, it will be way bigger help for me ! Thank You.

3 Answers3

0

change context to ViewGroup Context like below

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_fragment,parent,false);
    return new MyViewHolder(view);
}
Omkar
  • 3,040
  • 1
  • 22
  • 42
0

In my opinion, this issue is happening because "holder.phone_tv" is null. Hence you are calling setText(...) on a null object in line 32

Pls ensure that a view with id contact_number exists in the contact_fragment.xml layout.

I'm also suspecting that you aren't using the correct layout view for the recycler item holder. If this is the case, pls change contact_fragment.xml to the particular layout that holds the row items you wish to display in the adapter.

You can change it by editing this code block

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    view = LayoutInflater.from(context).inflate(**R.layout.recycler_item_layout**,parent,false);
    return new MyViewHolder(view);
}
Akinola Olayinka
  • 841
  • 7
  • 11
0

try this

  1. You did not initialize the list

     private List<Contact> data= new ArrayList<>();
    

or

  1. Check your layout file that all the widgets are there or not with the same name as you are passing in findviewbyid

     view = LayoutInflater.from(context).inflate(R.layout.item_contact,parent,false);
    
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43