'Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference
I am making an app that gets data from sql and puts it in a recycle view i first tried it in a list view but i found it easier in a recycle view it is basically like a chat system app anyways,when i add my data into the recycle view it gives me this error in the logcat.
java.lang.NullPointerException: Attempt to read from field
'android.view.View
androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a
null object reference
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7079)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:18010)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:18010)
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:18010)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
at android.view.View.layout(View.java:18010)
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:18010)
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)
Please help because it doesn't give me any clue on where is the error coming from or stuff like that.
here is my adapter class:
public class MyAppAdapter extends
RecyclerView.Adapter<MyAppAdapter.ViewHolder>//has a class viewholder which holds
{
private ArrayList<ClassListChat> mOriginalValues; // Original Values
private ArrayList<ClassListChat> mDisplayedValues;
public class ViewHolder extends RecyclerView.ViewHolder {
TextView messageText;
TextView messageUser;
TextView messageTime;
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
public List <ClassListChat> parkingList;
public Context context;
ArrayList<ClassListChat> arraylist;
private MyAppAdapter(List<ClassListChat> apps, Context context) {
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ClassListChat>();
arraylist.addAll(parkingList);
}
@Override
public MyAppAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listcontentstorechat,parent,false);
ViewHolder viewHolder = new ViewHolder(rowView);
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.listcontentstorechat, parent, false);
viewHolder.messageText = (BubbleTextView) rowView.findViewById(R.id.message_text);
viewHolder.messageUser = (TextView) rowView.findViewById(R.id.message_user);
viewHolder.messageTime = (TextView) rowView.findViewById(R.id.message_time);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
// here setting up names and images
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.messageText.setText(parkingList.get(position).getMessageText());
holder.messageUser.setText(parkingList.get(position).getMessageUser());
holder.messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", parkingList.get(position).getMessageTime()));
}
@Override
public int getItemCount() {
return itemArrayList.size();
}
}
I would really appreciate your hard work in helping me!!
EDIT: now it just gives me a blank bubbleTextView(which i implemented into my app)but it doesn't show the text,nor the time,nor the user any ideas?
Here is my listcontentstorechat.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:id="@+id/message_user"
android:textStyle="normal|bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_alignParentBottom="@+id/message_user"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:id="@+id/message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.github.library.bubbleview.BubbleTextView
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_below="@id/message_user"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#FFF"
android:textSize="18sp"
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
app:arrowWidth="8dp"
app:angle="8dp"
app:arrowHeight="10dp"
app:arrowPosition="14dp"
app:arrowLocation="left"
app:bubbleColor="#333639"
app:arrowCenter="true"
/>
</RelativeLayout>
And here is my ClassListChat.java
public class ClassListChat {
public String messageText;
public String messageUser;
public long messageTime;
public ClassListChat(String messageText, String messageUser) {
this.messageText = messageText;
this.messageUser = messageUser;
messageTime = new Date().getTime();
}
public ClassListChat() {
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getMessageUser() {
return messageUser;
}
public void setMessageUser(String messageUser) {
this.messageUser = messageUser;
}
public long getMessageTime() {
return messageTime;
}
public void setMessageTime(long messageTime) {
this.messageTime = messageTime;
}
EDIT2: After some improvements it now returns an error in the logcat saying:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:275)
at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:225)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
Solution 1:[1]
The ViewHolder
is always not null in onCreateViewHolder()
as the View
you created within this method can't be null.
onCreateViewHolder()
is built in a different way than getView()
that you use to createListView
adapter
So, please Change your onCreateViewHolder
to be
@Override
public MyAppAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listcontentstorechat,parent,false);
ViewHolder viewHolder = new ViewHolder(rowView);
return viewHolder;
}
Edit: now (and before actually) it doesn't add the text into the recycle view any help for that? I have updated my question
You return a wrong list size from the getItemCount()
, as your adapter uses arraylist
as its list of data, so you need to return the size of it
So change getItemCount()
into
@Override
public int getItemCount() {
if (arraylist != null)
return arraylist.size();
else
return 0;
}
Edit 2:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:275)
at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:225)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
You didn't initialize views within the row item layout using .findViewById()
, so change ViewHolder
constructor to be
public ViewHolder(@NonNull View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.message_text);
messageUser = itemView.findViewById(R.id.message_user);
messageTime = itemView.findViewById(R.id.message_time);
}
Solution 2:[2]
Your adapter is returning null viewholders.
View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listcontentstorechat,parent,false);
rowView
is always nonnull here, so this condition:
if (rowView == null) {
is never hit and instead the code goes to the else branch
} else { viewHolder = (ViewHolder) rowView.getTag(); }
that replaces viewHolder
with null and you return that.
That part of the code looks like an attempt to adapt and older ListView adapter to be a RecyclerView adapter. You don't need the setTag()
/getTag()
yourself with RecyclerView.
Just create and init your view holder and return that from the method.
Solution 3:[3]
I had the same issue, when I tried to use recyclerView
on a Fragment
to retrieve the data from firebase firestore and place it in the recyclerView
, I was not able to access the recyclerView
. Just add getView()
in front of findViewById(R.id.recyclerViewid)
My Code Before:
//Declared at the top of the class
RecylcerView recyclerView ;
//In the onViewCreate section
recyclerView = findViewById(R.id.recyclerview)//<--id of the recyclerView
My Code After:
//Declared at the top of the class
RecylcerView recyclerView ;
//In the onViewCreate section
recyclerView = (getView).findViewById(R.id.recyclerview)//<--id of the recyclerView
Solution 4:[4]
This is may be because your are not returning view through binding in setcontentview
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | laalto |
Solution 3 | Dale Julian |
Solution 4 | Gaming With Ishfaq |