linisme / SlimAdapter

A slim & clean & typeable Adapter without# VIEWHOLDER
https://raw.githubusercontent.com/MEiDIK/SlimAdapter/master/SlimAdapter.jpg
MIT License
931 stars 117 forks source link

Fatal Exception: java.lang.NullPointerException #22

Closed mradzinski closed 7 years ago

mradzinski commented 7 years ago

Hi. Some of my users are experiencing the following exception:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
       at net.idik.lib.slimadapter.SlimAdapter.getItemViewType(SlimAdapter.java:242)
       at net.idik.lib.slimadapter.SlimAdapterEx.getItemViewType(SlimAdapterEx.java:99)
       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5507)
       at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:282)
       at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:336)
       at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:349)
       at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:356)
       at android.support.v7.widget.GapWorker.run(GapWorker.java:387)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6688)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

It seems to be related to this line in particular. data.get(position); is returning null, hence dataTypes.indexOf(item.getClass()); fails with an NPE (item is null by that time).

Any idea what might be causing this?

linisme commented 7 years ago

i am sorry for recheck the issues so late,i am getting busy these days,, T T

it seems like the “data“ in the adapter has changed outside the adapter,as you see in the source, the "data" is just a refrence to the raw data set, so u should let the adapter knew when the raw data set changed with the "notifydatasetchanged()" method. Hope this helps.