M
M
Maxim Siomin2021-04-23 17:22:29
Kotlin
Maxim Siomin, 2021-04-23 17:22:29

Error java.lang.NullPointerException: Attempt to invoke virtual method?

I have a code

package com.maximsiomin.batterytemperature.battery<br>
<br>
import android.content.Context<br>
import android.content.Intent<br>
import android.content.IntentFilter<br>
import android.os.BatteryManager<br>
import com.maximsiomin.batterytemperature.ui.AppCompat<br>
<br>
//Here program gets current battery temperature of device<br>
fun getBatteryTemperature(): Int {<br>
    return AppCompat().applicationContext.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))!!.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10<br>
}<br>

Here is the AppCompat class
package com.maximsiomin.batterytemperature.ui

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.maximsiomin.batterytemperature.R
import com.maximsiomin.batterytemperature.viewmodel.AppViewModel

class AppCompat : AppCompatActivity() {
    private val temperature by lazy { ViewModelProviders.of(this).get(AppViewModel::class.java)}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val temperatureTextView: TextView = findViewById(R.id.temperature_number)
        val temperatureObserver = Observer<Int> { currentTemperature -> temperatureTextView.text = currentTemperature.toString() }
        temperature.currentTemperature.observe(this, temperatureObserver)
    }
}

And here is the error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maximsiomin.batterytemperature/com.maximsiomin.batterytemperature.ui.AppCompat}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        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:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:118)
        at com.maximsiomin.batterytemperature.battery.BatteryKt.getBatteryTemperature(battery.kt:13)
        at com.maximsiomin.batterytemperature.viewmodel.AppViewModel.<init>(AppViewModel.kt:14)
        at java.lang.Class.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:182)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:241)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
        at com.maximsiomin.batterytemperature.ui.AppCompat$temperature$2.invoke(AppCompat.kt:12)
        at com.maximsiomin.batterytemperature.ui.AppCompat$temperature$2.invoke(AppCompat.kt:11)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at com.maximsiomin.batterytemperature.ui.AppCompat.getTemperature(Unknown Source:2)
        at com.maximsiomin.batterytemperature.ui.AppCompat.onCreate(AppCompat.kt:20)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        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:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

How to fix? I found several answers on stackoverflow, but they did not help

. UPD: in kotlin, the habr code added br. How to remove - I do not know

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2021-04-23
@zagayevskiy

There is definitely an error in the line "AppCompat().applicationContext...". There is not a single situation when you would need to create activities manually. Activity is always created by the system. If the context is necessary - accept it in function outside.

O
OneDeus, 2022-01-18
@OneDeus

https://stackoverflow.com/questions/5974063/how-to...
and then from java to kotlin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question