0

Command line

val /*Variable Name*/ = findviewbyid<ImageButton>(R.id./*id*/)

Causing runtime error in Emulator running Android 11

Error Log:

 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cobalt.coffetra/com.cobalt.coffetra.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        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:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:173)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
        at android.content.Context.obtainStyledAttributes(Context.java:744)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:819)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:261)
        at com.cobalt.coffetra.MainActivity.<init>(MainActivity.kt:81)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        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:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Tried changing Gradle Version, but did not work

z.g.y
  • 5,512
  • 4
  • 10
  • 36
  • 1
    this is the relevant part of that stacktrace: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at – Stultuske Nov 10 '22 at 11:11
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Nov 10 '22 at 11:11
  • it should like- val imageButton:ImageButton = findViewById(R.id.ImageButton) – Sandesh Khutal Nov 10 '22 at 11:20
  • In Kotlin?? will try – Cobaltboy Nov 10 '22 at 11:44
  • 1
    You cannot call `findViewById()` in a property initializer like that: https://stackoverflow.com/q/36446114. You need to rearrange your code so that the `findViewById()` call does not happen until after the `setContentView()` call; e.g., possibly by changing your variable to a `lateinit var` and moving the call, or maybe by using `by lazy`, etc. – Mike M. Nov 10 '22 at 14:10
  • You can assign the result of a `findViewById` to a variable in that way, depending on the point in the lifecycle. If you could share a bit more of the class that would be helpful – Sam Nov 10 '22 at 17:34
  • @Sam it's definitely happening during initialisation in this case, that's why it's calling `getApplicationInfo()` on a null `Context`, which an Activity doesn't have until later. Mike's right about using `lateinit` or `lazy` here (although if you use `lazy`, you have to make sure anything that references it either happens in/after `onCreate`, or that it's `lazy` itself) – cactustictacs Nov 10 '22 at 20:53
  • @Stultuske no, it really isn't...that's framework code. You need to look at the part of the stack trace involving the user's code. – Ryan M Nov 11 '22 at 22:59

0 Answers0