title: findViewById() must not be null
body: <p>I'm trying to make a navigation drawer. I've made the layout file for the drawer, assigned the views appropriate IDs, and then in the kotlin file I've declared the variables corresponding to the Views using the <code>lateinit</code> keyword. Inside the <code>onCreate()</code> method I've initialised these variables using <code>findViewById(R.id...)</code></p>
  <p>When I run the code, the app crashes and the logcat shows me a <strong>NullPointerException</strong> saying that <strong>findViewById(R.id.drawerLayout) must not be null</strong>. Here is my code for reference:</p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
  lateinit var drawerLayout: DrawerLayout
  lateinit var coordinatorLayout: CoordinatorLayout
  lateinit var toolbar: Toolbar
  lateinit var frameLayout: FrameLayout
  lateinit var navigationView: NavigationView
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      drawerLayout = findViewById(R.id.drawerLayout)
      coordinatorLayout = findViewById(R.id.coordinatorLayout)
      toolbar = findViewById(R.id.toolbar)
      frameLayout = findViewById(R.id.frameLayout)
      navigationView = findViewById(R.id.navigationView)
      setUpToolbar()
  
      val actionBarDrawerToggle = ActionBarDrawerToggle(
          this@MainActivity,
          drawerLayout,
          R.string.open_drawer,
          R.string.close_drawer)
  
      drawerLayout.addDrawerListener(actionBarDrawerToggle)    
      actionBarDrawerToggle.syncState()
  }
  
  
  fun setUpToolbar(){
      supportActionBar?.title = &quot;Toolbar Title&quot;       
      supportActionBar?.setHomeButtonEnabled(true)
      supportActionBar?.setDisplayHomeAsUpEnabled(true) 
  
  
   }
  }
  </code></pre>
  <p>This is my xml code:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:id=&quot;@+id/drawerLayout&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
  &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
      android:id=&quot;@+id/coordinatorLayout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;androidx.appcompat.widget.Toolbar
          android:id=&quot;@+id/toolbar&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:minHeight=&quot;?attr/actionBarSize&quot;
          android:background=&quot;@color/design_default_color_primary_dark&quot;
          android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark&quot;/&gt;
  
  
      &lt;FrameLayout
          android:id=&quot;@+id/frameLayout&quot;
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;fill_parent&quot;/&gt;
  
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  
      &lt;com.google.android.material.navigation.NavigationView
          android:id=&quot;@+id/navigationView&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          app:headerLayout=&quot;@layout/drawer_header&quot;
          app:menu=&quot;@menu/menu_drawer&quot;
          android:layout_gravity = &quot;start&quot;/&gt;
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  </code></pre>
  <p>I've tried almost everything under the sun, including</p>
  <ol>
  <li><p>declaring the variables in this way: <code>var drawerLayout: DrawerLayout? = null</code>, which helps me avoid the <strong>nullPointerException</strong> in the findViewById line but gives me a whole other level of hell in further part of my code where I have to add click listeners to the toggle using <code>drawerLayout?.addDrawerListener(actionBarDrawerToggle)</code>. I asked a <a href="https://stackoverflow.com/questions/68794744/smart-cast-to-drawerlayout-is-impossible-because-drawerlayout-is-a-mutable">question here on stackoverflow</a> regarding this error I face with the click listener and the general consensus was to not use this way of declaring <code>drawerLayout</code> so as to not make it nullable.</p>
  </li>
  <li><p>I've tried using the non null asserted call this way: <code>var drawerLayout: DrawerLayout!!</code> but it still throws me the same error</p>
  </li>
  <li><p>I've checked and rechecked and checked and rechecked the ID of my <code>DrawerLayout</code> tag in the layout file since everyone says the exception might be due to the ID and the variable name being different but that's not the case as well. I've even cleared and rebuilt the project, invalidated caches, still no change.</p>
  </li>
  </ol>
  <p>I have no idea how to proceed with this. I've been stuck with this error for the longest time and nothing seems to be working. It'd be great if someone could point out where I'm going wrong</p>