Solution 1 :
You have to access intent
only after onCreate
is called. You can fix it by wrapping into lazy
delegate like this:
val player by lazy { intent.getParcelableExtra<Player>(EXTRA_PLAYER) }
Problem :
I was simply creating this app for practice when I ran into NullPointerException
but my code was working just moments ago and I have no idea why this happened.
I was implementing the onSaveInstanceState
and onRestoreInstanceState
for the orientation change and the player
variable on line 14 in SkillActivity.kt
stopped working and, thus, my app keeps crashing. Here’s the code in SkillActivity.kt
:
package com.example.soosh.controller
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.Toast
import com.example.soosh.Model.Player
import com.example.soosh.R
import com.example.soosh.utlities.EXTRA_PLAYER
import kotlinx.android.synthetic.main.activity_skill.*
class SkillActivity : BaseActivity() {
var player = intent.getParcelableExtra<Player>(EXTRA_PLAYER)
override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
super.onSaveInstanceState(outState, outPersistentState)
outState.putParcelable(EXTRA_PLAYER, player)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_skill)
finishBtn.setOnClickListener {
if (player?.category != "") {
val finished = Intent(this, FinishedActivity::class.java)
finished.putExtra(EXTRA_PLAYER, player)
startActivity(finished)
}
else {
val popup = Toast.makeText(this, "Please select a category to continue.", Toast.LENGTH_LONG)
popup.show()
}
}
beginnerButton.setOnClickListener {
ballerButton.isChecked = false
player?.category = "Beginner"
}
ballerButton.setOnClickListener {
beginnerButton.isChecked = false
player?.category = "Baller"
}
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
if (savedInstanceState != null) {
player = savedInstanceState.getParcelable(EXTRA_PLAYER)
}
}
}
The error in logcat:
06-07 19:07:18.973 7773-7773/com.example.soosh E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.soosh, PID: 7773
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.soosh/com.example.soosh.controller.SkillActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.content.Intent.getParcelableExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3007)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6918)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.content.Intent.getParcelableExtra(java.lang.String)' on a null object reference
at com.example.soosh.controller.SkillActivity.<init>(SkillActivity.kt:14)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1689)
at android.app.Instrumentation.newActivity(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2997)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6918)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Any help is really appreciated. Thanks 🙂