'java.lang.NullPointerException: DataBindingUtil.setConte….layout.activity_chapter) must not be null

I have just build a project with android kotlin but when I run the app it got crash, and in the logcat I found this error

java.lang.NullPointerException: DataBindingUtil.setConte….layout.activity_chapter) must not be null

ChapterActivity.kt

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.tutorial.kotlinfirebasecomicreader.Common.Common
import com.tutorial.kotlinfirebasecomicreader.Model.Comic
import com.tutorial.kotlinfirebasecomicreader.databinding.ActivityChapterBinding

class ChapterActivity : AppCompatActivity() {

private lateinit var binding: ActivityChapterBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_chapter)


    binding.toolbar.title = Common.selected_comic!!.Name
    binding.toolbar.setNavigationIcon(R.drawable.ic_baseline_chevron_left_24)
    binding.toolbar.setNavigationOnClickListener {
        finish() //GO BACK
    }

    binding.recyclerChapter.setHasFixedSize(true)
    val layoutManager = LinearLayoutManager(this@ChapterActivity)
    binding.recyclerChapter.layoutManager = layoutManager
    binding.recyclerChapter.addItemDecoration(DividerItemDecoration(this, layoutManager.orientation))

    fetchChapter (Common.selected_comic!!)
}

private fun fetchChapter(comic: Comic) {
    Common.chapterList = comic.Chapters!!
    binding.txtChapterName.text = StringBuilder("Chapter (")
            .append(comic.Chapters!!.size)
            .append(")")
}
}

I don't really know how to fix it and I need some help

Edited => activity_chapter.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

</data>

<LinearLayout
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChapterActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        app:titleTextColor="@color/textColor"
        android:background="@color/columnColor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:padding="16dp"
        android:weightSum="4"
        android:background="@color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txt_chapter_name"
            android:text="Chapter (10)"
            android:textColor="@color/textColor"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_chapter"
        android:padding="8dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
</layout>

Stack Trace =>

2021-03-04 11:55:23.033 11911-11911/? I/basecomicreade: Not late-enabling -Xcheck:jni (already on)
2021-03-04 11:55:23.106 11911-11911/? I/basecomicreade: Unquickening 13 vdex files!
2021-03-04 11:55:23.108 11911-11911/? W/basecomicreade: Unexpected CPU variant for X86 using defaults: x86_64
2021-03-04 11:55:23.913 11911-11911/com.tutorial.kotlinfirebasecomicreader W/ComponentDiscovery: Class com.google.firebase.dynamicloading.DynamicLoadingRegistrar is not an found.
2021-03-04 11:55:23.933 11911-11911/com.tutorial.kotlinfirebasecomicreader I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
2021-03-04 11:55:23.957 11911-11911/com.tutorial.kotlinfirebasecomicreader I/FirebaseInitProvider: FirebaseApp initialization successful
2021-03-04 11:55:24.001 11911-11932/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Unsupported class loader
2021-03-04 11:55:24.010 11911-11932/com.tutorial.kotlinfirebasecomicreader I/DynamiteModule: Considering local module com.google.android.gms.measurement.dynamite:46 and remote module com.google.android.gms.measurement.dynamite:45
2021-03-04 11:55:24.010 11911-11932/com.tutorial.kotlinfirebasecomicreader I/DynamiteModule: Selected local version of com.google.android.gms.measurement.dynamite
2021-03-04 11:55:24.053 11911-11937/com.tutorial.kotlinfirebasecomicreader D/libEGL: loaded /vendor/lib64/egl/libEGL_emulation.so
2021-03-04 11:55:24.055 11911-11937/com.tutorial.kotlinfirebasecomicreader D/libEGL: loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so
2021-03-04 11:55:24.062 11911-11937/com.tutorial.kotlinfirebasecomicreader D/libEGL: loaded /vendor/lib64/egl/libGLESv2_emulation.so
2021-03-04 11:55:24.160 11911-11911/com.tutorial.kotlinfirebasecomicreader D/skia: Warning: an opaque image should be decoded as opaque - it is being decoded as non-opaque, which will draw slower
2021-03-04 11:55:24.200 11911-11932/com.tutorial.kotlinfirebasecomicreader V/FA: onActivityCreated
2021-03-04 11:55:24.250 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: App measurement collection enabled
2021-03-04 11:55:24.254 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: App measurement enabled for app package, google app id: com.tutorial.kotlinfirebasecomicreader, 1:705167416733:android:e2429adee28166ea782374
2021-03-04 11:55:24.256 11911-11938/com.tutorial.kotlinfirebasecomicreader I/FA: App measurement initialized, version: 39000
2021-03-04 11:55:24.256 11911-11938/com.tutorial.kotlinfirebasecomicreader I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2021-03-04 11:55:24.257 11911-11938/com.tutorial.kotlinfirebasecomicreader I/FA: To enable faster debug mode event logging run:
  adb shell setprop debug.firebase.analytics.app com.tutorial.kotlinfirebasecomicreader
2021-03-04 11:55:24.257 11911-11938/com.tutorial.kotlinfirebasecomicreader D/FA: Debug-level message logging enabled
2021-03-04 11:55:24.287 11911-11911/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-03-04 11:55:24.288 11911-11911/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-03-04 11:55:24.354 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connecting to remote service
2021-03-04 11:55:24.420 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connection attempt already in progress
2021-03-04 11:55:24.476 11911-11935/com.tutorial.kotlinfirebasecomicreader D/HostConnection: HostConnection::get() New Host Connection established 0x7d323924d620, tid 11935
2021-03-04 11:55:24.490 11911-11935/com.tutorial.kotlinfirebasecomicreader D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 
2021-03-04 11:55:24.507 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connection attempt already in progress
2021-03-04 11:55:24.509 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Activity resumed, time: 57788511
2021-03-04 11:55:24.600 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglCreateContext: 0x7d323924e840: maj 3 min 0 rcv 3
2021-03-04 11:55:24.680 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:24.690 11911-11938/com.tutorial.kotlinfirebasecomicreader I/FA: Tag Manager is not found and thus will not be used
2021-03-04 11:55:24.706 11911-11935/com.tutorial.kotlinfirebasecomicreader I/Gralloc4: mapper 4.x is not supported
2021-03-04 11:55:24.707 11911-11935/com.tutorial.kotlinfirebasecomicreader W/Gralloc3: mapper 3.x is not supported
2021-03-04 11:55:24.712 11911-11935/com.tutorial.kotlinfirebasecomicreader D/HostConnection: createUnique: call
2021-03-04 11:55:24.712 11911-11935/com.tutorial.kotlinfirebasecomicreader D/HostConnection: HostConnection::get() New Host Connection established 0x7d323924eac0, tid 11935
2021-03-04 11:55:24.713 11911-11935/com.tutorial.kotlinfirebasecomicreader D/eglCodecCommon: allocate: Ask for block of size 0x100
2021-03-04 11:55:24.713 11911-11935/com.tutorial.kotlinfirebasecomicreader D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3fe042000 size 0x2000
2021-03-04 11:55:24.716 11911-11935/com.tutorial.kotlinfirebasecomicreader D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_0 
2021-03-04 11:55:24.730 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:24.795 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connection attempt already in progress
2021-03-04 11:55:24.797 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connection attempt already in progress
2021-03-04 11:55:25.306 11911-11935/com.tutorial.kotlinfirebasecomicreader I/OpenGLRenderer: Davey! duration=900ms; Flags=1, IntendedVsync=57788545708266, Vsync=57788579041598, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=57788595779340, AnimationStart=57788595852830, PerformTraversalsStart=57788595904150, DrawStart=57788869782150, SyncQueued=57788872423620, SyncStart=57788883887770, IssueDrawCommandsStart=57788884972440, SwapBuffers=57789368117580, FrameCompleted=57789457618760, DequeueBufferDuration=307500, QueueBufferDuration=1103560, GpuCompleted=0, 
2021-03-04 11:55:25.576 11911-11938/com.tutorial.kotlinfirebasecomicreader D/FA: Connected to remote service
2021-03-04 11:55:25.577 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Processing queued up service tasks: 5
2021-03-04 11:55:27.569 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Recording user engagement, ms: 3024
2021-03-04 11:55:27.599 11911-11932/com.tutorial.kotlinfirebasecomicreader V/FA: onActivityCreated
2021-03-04 11:55:27.787 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Activity paused, time: 57791583
2021-03-04 11:55:27.924 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Activity resumed, time: 57792074
2021-03-04 11:55:27.986 11911-11946/com.tutorial.kotlinfirebasecomicreader D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-03-04 11:55:27.994 11911-11911/com.tutorial.kotlinfirebasecomicreader E/RecyclerView: No adapter attached; skipping layout
2021-03-04 11:55:28.090 11911-11946/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Verification of java.util.Set com.android.org.conscrypt.SSLUtils.getSupportedClientKeyTypes(byte[], int[]) took 100.226ms (648.53 bytecodes/s) (3304B approximate peak alloc)
2021-03-04 11:55:28.260 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:28.732 11911-11946/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Verification of boolean com.android.org.conscrypt.CertBlacklistImpl.isHex(java.lang.String) took 124.689ms (288.72 bytecodes/s) (2168B approximate peak alloc)
2021-03-04 11:55:28.836 11911-11911/com.tutorial.kotlinfirebasecomicreader I/Choreographer: Skipped 51 frames!  The application may be doing too much work on its main thread.
2021-03-04 11:55:28.880 11911-11930/com.tutorial.kotlinfirebasecomicreader I/basecomicreade: Waiting for a blocking GC ProfileSaver
2021-03-04 11:55:28.887 11911-11930/com.tutorial.kotlinfirebasecomicreader I/basecomicreade: WaitForGcToComplete blocked ProfileSaver on RunEmptyCheckpoint for 7.720ms
2021-03-04 11:55:29.270 11911-11935/com.tutorial.kotlinfirebasecomicreader I/OpenGLRenderer: Davey! duration=1239ms; Flags=0, IntendedVsync=57792137367431, Vsync=57792987367397, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=57792988665860, AnimationStart=57792988801340, PerformTraversalsStart=57792989546070, DrawStart=57792989762430, SyncQueued=57792990250570, SyncStart=57792999374600, IssueDrawCommandsStart=57792999429610, SwapBuffers=57793072211200, FrameCompleted=57793385836850, DequeueBufferDuration=20442430, QueueBufferDuration=76115660, GpuCompleted=77309411328, 
2021-03-04 11:55:29.691 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:29.792 11911-11935/com.tutorial.kotlinfirebasecomicreader I/OpenGLRenderer: Davey! duration=1803ms; Flags=1, IntendedVsync=57792137367431, Vsync=57792987367397, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=57792988665860, AnimationStart=57792988801340, PerformTraversalsStart=57792989546070, DrawStart=57793838734440, SyncQueued=57793840519500, SyncStart=57793843650750, IssueDrawCommandsStart=57793843800190, SwapBuffers=57793936524990, FrameCompleted=57793943762960, DequeueBufferDuration=82930, QueueBufferDuration=274660, GpuCompleted=8810055295132672, 
2021-03-04 11:55:29.797 11911-11911/com.tutorial.kotlinfirebasecomicreader I/Choreographer: Skipped 57 frames!  The application may be doing too much work on its main thread.
2021-03-04 11:55:29.887 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:30.096 11911-11935/com.tutorial.kotlinfirebasecomicreader I/OpenGLRenderer: Davey! duration=1062ms; Flags=0, IntendedVsync=57792995727619, Vsync=57793945727581, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=57793949431810, AnimationStart=57793949481480, PerformTraversalsStart=57793952149640, DrawStart=57793952476430, SyncQueued=57793953226090, SyncStart=57794142461760, IssueDrawCommandsStart=57794149759670, SwapBuffers=57794236027780, FrameCompleted=57794247497260, DequeueBufferDuration=296420, QueueBufferDuration=1234510, GpuCompleted=137654660947320, 
2021-03-04 11:55:30.221 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:30.455 11911-11935/com.tutorial.kotlinfirebasecomicreader I/OpenGLRenderer: Davey! duration=1270ms; Flags=0, IntendedVsync=57792995727619, Vsync=57793945727581, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=57793949431810, AnimationStart=57793949481480, PerformTraversalsStart=57793952149640, DrawStart=57794142711790, SyncQueued=57794142809910, SyncStart=57794483167180, IssueDrawCommandsStart=57794483392980, SwapBuffers=57794595122060, FrameCompleted=57794606716760, DequeueBufferDuration=131380, QueueBufferDuration=409730, GpuCompleted=137657109016869, 
2021-03-04 11:55:30.456 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:30.511 11911-11911/com.tutorial.kotlinfirebasecomicreader D/Slider: onImageSlideChange() called with: position = [0]
2021-03-04 11:55:30.511 11911-11911/com.tutorial.kotlinfirebasecomicreader I/SlideIndicatorsGroup: onSlideChange: 0
2021-03-04 11:55:30.515 11911-11911/com.tutorial.kotlinfirebasecomicreader I/SlideIndicatorsGroup: onSlideChange: 0
2021-03-04 11:55:30.525 11911-11911/com.tutorial.kotlinfirebasecomicreader I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
2021-03-04 11:55:30.608 11911-11911/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, reflection, allowed)
2021-03-04 11:55:30.608 11911-11911/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
2021-03-04 11:55:30.608 11911-11911/com.tutorial.kotlinfirebasecomicreader W/basecomicreade: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)
2021-03-04 11:55:30.718 11911-11935/com.tutorial.kotlinfirebasecomicreader D/EGL_emulation: eglMakeCurrent: 0x7d323924e840: ver 3 0 (tinfo 0x7d32392d78c0)
2021-03-04 11:55:31.073 11911-11935/com.tutorial.kotlinfirebasecomicreader I/OpenGLRenderer: Davey! duration=1118ms; Flags=1, IntendedVsync=57793962375754, Vsync=57794662375726, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=57794676994830, AnimationStart=57794677072830, PerformTraversalsStart=57794677609830, DrawStart=57794835905760, SyncQueued=57794837936740, SyncStart=57794982254730, IssueDrawCommandsStart=57794985287860, SwapBuffers=57795092878620, FrameCompleted=57795224849780, DequeueBufferDuration=215540, QueueBufferDuration=11529020, GpuCompleted=0, 
2021-03-04 11:55:33.429 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Inactivity, disconnecting from the service
2021-03-04 11:55:35.432 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Recording user engagement, ms: 7288
2021-03-04 11:55:35.455 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connecting to remote service
2021-03-04 11:55:35.460 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Activity paused, time: 57799379
2021-03-04 11:55:35.589 11911-11932/com.tutorial.kotlinfirebasecomicreader V/FA: onActivityCreated
2021-03-04 11:55:35.668 11911-11911/com.tutorial.kotlinfirebasecomicreader D/AndroidRuntime: Shutting down VM
2021-03-04 11:55:35.669 11911-11911/com.tutorial.kotlinfirebasecomicreader E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tutorial.kotlinfirebasecomicreader, PID: 11911
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tutorial.kotlinfirebasecomicreader/com.tutorial.kotlinfirebasecomicreader.ChapterActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7506)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.intern
2021-03-04 11:55:35.782 11911-11938/com.tutorial.kotlinfirebasecomicreader V/FA: Connection attempt already in progress


Solution 1:[1]

The documentation says that DataBindingUtil.setContentView returns:

The binding associated with the inflated content view or null if the layoutId is not a data binding layout.

So I would have to assume that your layout is not a valid data binding layout, so the issue lies in your XML.

I would recommend reading: Binding expressions which shows you the syntax of a valid binding layout and also this: Data Binding Library fully to help get a comprehensive understanding.

Also the answer by @Teo does point out the recommended way to use data binding so unless you have a specific reason for using the generic version I would switch to that.

Solution 2:[2]

Can try to avoid using DataBindingUtil as it keeps return null in my case.

You can try using its own databinding.inflate, as it will always get the correct binding.

binding = ActivityChapterBinding.inflate(layoutInflater)
setContentView(binding.root)

Solution 3:[3]

I've done Build > Clean Project and it worked. I guess there was a problem with generated classes.

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 Teo
Solution 3 Ercan Akkök