Why My code throws a null pointer exception ??? I have initialized all the variables i have declared,

-1

The App comprises of a login Page and a scroll type activity, where the user inputs its credentials and then gets logged in with the name as its title . Also.. if the user is logged in before, it stays logged in even after exiting the app. Here is the Kotlin file of Login Page.

package com.internshla.myapplication

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
//import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast



class MainActivity : AppCompatActivity() {


    private val pIN = "5"
    private val p1  = "a"
    private val p2  = "b"
    private val p3  = "c"

    private lateinit var etpinGet: EditText
    private lateinit var etpasswordGet: EditText
    private lateinit var btEnter: Button

    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println(" 8888 On create called!!! 8888 ")
        setContentView(R.layout.activity_main)

        sharedPreferences = getSharedPreferences(getString(R.string.preference_file_name),Context.MODE_PRIVATE)



        val isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false)

        if (isLoggedIn) {
            
            print("user is logged in before")
            startActivity(intent)
            finish()
        }

        btEnter = findViewById(R.id.btEnter)
        btEnter.setOnClickListener {
            val intent = Intent(
                this@MainActivity,
                ScrollActivity::class.java
            )   

            print("button clicked 9999")
            etpinGet = findViewById(R.id.etpinGet)
            etpasswordGet = findViewById(R.id.etpasswordGet)

            val pin = etpinGet.text.toString()   
            val pass = etpasswordGet.text.toString()


            if (pin == pIN) {

                if (pass == p1) {
                    savePreferencesFun("P1 Password", "Welcome P1 user")
                    intent.putExtra("title", p1)     
                    
                    startActivity(intent)
                } else if (pass == p2) {
                    savePreferencesFun("P2 Password", "Welcome P2 user")
                    intent.putExtra("title", p2)
                    
                    startActivity(intent)
                } else if (pass == p3) {
                    savePreferencesFun("P3 Password", "Welcome P3 user")
                    intent.putExtra("title", p3)
                    
                    startActivity(intent)
                } else {
                    Toast.makeText(
                        this@MainActivity,
                        "Incorrect Password ! ",
                        Toast.LENGTH_SHORT
                    ).show()
                }

            } else {
                Toast.makeText(
                    this@MainActivity,
                    "Incorrect Pin ",
                    Toast.LENGTH_SHORT
                ).show()

            }

        }

    }

    private fun savePreferencesFun(
        name: String,
        toast1: String
    ) {
                             
        sharedPreferences.edit().putBoolean("isLoggedIn", true).apply()
        sharedPreferences.edit().putString("Name", name).apply()
        sharedPreferences.edit().putString("Toast", toast1).apply()
    }

    override fun onPause() {    after scrolling
        super.onPause()
        finish()
    }

Kotlin file of Scroll activity

package com.internshla.myapplication
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

class ScrollActivity : AppCompatActivity() {

      private lateinit var bt:Button
      lateinit var sharedPref: SharedPreferences

     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_scroll)
         print("onCreate called!!!")

         sharedPref =
            getSharedPreferences(getString(R.string.preference_file_name), Context.MODE_PRIVATE)
         title=sharedPref.getString("Name","LOL")

        //if(intent!=null) {              
        //     title = intent.getStringExtra("title")
        // }

        val getToast=sharedPref.getString("Toast","welcome new user")   
        Toast.makeText(
            this@ScrollActivity,    //make that toast
            getToast,
            Toast.LENGTH_SHORT
        ).show()

        bt= findViewById(R.id.logoutbtn)
        bt.setOnClickListener{
            val intBack= Intent (this@ScrollActivity,MainActivity::class.java)
            startActivity(intBack)
            sharedPref.edit().clear().apply()
        }

    }

}

Here is my Manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.internshla.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ScrollActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I am an absolute beginner in App Development , and stuck in this for over 5 days. So please help me with this issue. And if there is any improvement that you can recommend me other than solving error, then it will be even a better learning experience for me.Thank you!

edit Here is Logcat :

2020-07-15 02:03:50.652 27888-27888/? I/a.myapplicatio: Late-enabling -Xcheck:jni
2020-07-15 02:03:50.671 27888-27888/? E/a.myapplicatio: Unknown bits set in runtime_flags: 0x28000
2020-07-15 02:03:50.781 27888-27888/com.internshla.myapplication I/Perf: Connecting to perf service.
2020-07-15 02:03:50.801 27888-27917/com.internshla.myapplication E/Perf: Fail to get file list com.internshla.myapplication
2020-07-15 02:03:50.801 27888-27917/com.internshla.myapplication E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-07-15 02:03:50.802 27888-27917/com.internshla.myapplication E/Perf: Fail to get file list com.internshla.myapplication
2020-07-15 02:03:50.802 27888-27917/com.internshla.myapplication E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-07-15 02:03:50.803 27888-27917/com.internshla.myapplication E/Perf: Fail to get file list oat
2020-07-15 02:03:50.803 27888-27917/com.internshla.myapplication E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-07-15 02:03:50.837 27888-27888/com.internshla.myapplication I/System.out:  8888 On create called!!! 8888 
2020-07-15 02:03:50.841 27888-27888/com.internshla.myapplication E/a.myapplicatio: Invalid ID 0x00000000.
2020-07-15 02:03:50.880 27888-27888/com.internshla.myapplication W/a.myapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-07-15 02:03:50.881 27888-27888/com.internshla.myapplication W/a.myapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-07-15 02:03:50.965 27888-27888/com.internshla.myapplication D/WindowManager: Add to mViews: DecorView@d2df9ef[MainActivity], this = android.view.WindowManagerGlobal@c946cfc,pkg= com.internshla.myapplication
2020-07-15 02:03:51.014 27888-27918/com.internshla.myapplication I/AdrenoGLES: QUALCOMM build                   : e4029f9, I6b4cbc7431
    Build Date                       : 10/01/19
    OpenGL ES Shader Compiler Version: EV031.27.05.01
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
2020-07-15 02:03:51.014 27888-27918/com.internshla.myapplication I/AdrenoGLES: Build Config                     : S P 8.0.11 AArch64
2020-07-15 02:03:51.017 27888-27918/com.internshla.myapplication I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
2020-07-15 02:03:51.019 27888-27918/com.internshla.myapplication W/AdrenoUtils: <ReadGpuID_from_sysfs:194>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
2020-07-15 02:03:51.019 27888-27918/com.internshla.myapplication W/AdrenoUtils: <ReadGpuID:218>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
2020-07-15 02:03:51.035 27888-27918/com.internshla.myapplication W/Gralloc3: mapper 3.x is not supported
2020-07-15 02:03:51.060 27888-27888/com.internshla.myapplication I/Choreographer: Skipped 4 frames!  The application may be doing too much work on its main thread.
android
kotlin
android-intent
nullpointerexception
sharedpreferences
asked on Stack Overflow Jul 14, 2020 by Vaishnav Dixit • edited Jul 17, 2020 by Ryan M

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0