Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Java NullPointerException in Android Studio

Posted on November 11, 2022 By

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 🙂

READ  [FIXED] android - Kotlin: start an Activity from a fragment
Powered by Inline Related Posts
Android Tags:android, android-studio, kotlin, nullpointerexception

Post navigation

Previous Post: [FIXED] android – Can you get the tag of a date picker
Next Post: [FIXED] Android Recyclerview add item and notifyiteminserted causes blink recycled view before draw the correct

Related Posts

[FIXED] android – How to get direct Google play store app download link for user? Android
[FIXED] listview – android – Kotlin – how to access a solid color inside a background’s custom Button Android
[FIXED] android – Problems with insertions after adding new tables to database Android
[FIXED] java – how to set volume of media player between 1 to 100% in android studio? Android
[FIXED] android – How to change programmatically a Contact from phone? Android
[FIXED] android – SQLite query for combining where, oder by and union all Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme