Nicoara
Nicoara

Reputation: 390

RunTimeException unable to instantiate activity ComponentInfo{...}: java.lang.NullPointerException:

My app worked just fine, i could go from one activity to another then i add some Toast messages for Registration button then i got this error when i'm trying to go from MainActivity to RegistrationActivity..

MainActivity.kt

import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.io.FileReader
import java.io.InputStream
import java.util.jar.Manifest

class MainActivity : AppCompatActivity() {

    // private val STORAGE_PERMISSION_CODE = 1

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

        btnLogin.setOnClickListener {
            Toast.makeText(this,"Some message", Toast.LENGTH_SHORT).show()
        }

        tvGoToRegistration.setOnClickListener{
            val intent = Intent(this, RegistrationActivity::class.java)
            startActivity(intent)
        }
    }

RegistrationActivity.kt

package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_registration.*

class RegistrationActivity : AppCompatActivity(){

    val userName = etUserName.text.toString()
    val userEmail = etUserEmail.text.toString()
    val userPassword = etPassword.text.toString()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_registration)

        btnRegistration.setOnClickListener {
            if(validate() == false)
                 Toast.makeText(this,"Incomplete data", Toast.LENGTH_SHORT).show()
             else
             {
                 Toast.makeText(this, "Registration successed", Toast.LENGTH_SHORT).show()
                 // update database
             }
        }
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.569" />

    <TextView
        android:id="@+id/tvGoToRegistration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to registration"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnLogin" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_registraion.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <EditText
        android:id="@+id/etUserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="name.."
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.155" />

    <EditText
        android:id="@+id/etUserEmail"
        style="@android:style/Widget.DeviceDefault.Light.EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="email.."
        android:inputType="textEmailAddress"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etUserName"
        app:layout_constraintVertical_bias="0.147" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="password.."
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etUserEmail"
        app:layout_constraintVertical_bias="0.165" />

    <TextView
        android:id="@+id/tvBackToLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Already sign in? Login"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnRegistration" />

    <Button
        android:id="@+id/btnRegistration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etPassword"
        app:layout_constraintVertical_bias="0.383" />
</androidx.constraintlayout.widget.ConstraintLayout>

manifest

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

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".RegistrationActivity"/>
        <activity android:name=".Test"/>
    </application>

</manifest> 

And logchat

2019-12-09 09:08:16.171 28691-28691/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 28691 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication/com.example.myapplication.RegistrationActivity}: 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:2548) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 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:149) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157) at android.content.Context.obtainStyledAttributes(Context.java:575) at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659) at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214) at com.example.myapplication.RegistrationActivity._$_findCachedViewById(RegistrationActivity.kt) at com.example.myapplication.RegistrationActivity.(RegistrationActivity.kt:10) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1079) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

I think the error is caused by startActivity(intent) in MainActivity but when i run it in debbuger i can go past that line but i can't enter the RegistrationActivity class

Upvotes: 0

Views: 8036

Answers (2)

The Bat
The Bat

Reputation: 1105

I believe the issue because you are trying to get the text from the EditTexts before onCreate of the activity.

enter image description here

If you move this code inside onCreate, it should work.

package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_registration.*

class RegistrationActivity : AppCompatActivity(){


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

    //HERE AFTER setContentView, THE LAYOUT IS RENEDERED

    val userName = etUserName.text.toString()
    val userEmail = etUserEmail.text.toString()
    val userPassword = etPassword.text.toString()

    btnRegistration.setOnClickListener {
        if(validate() == false)
             Toast.makeText(this,"Incomplete data", Toast.LENGTH_SHORT).show()
         else
         {
             Toast.makeText(this, "Registration successed", Toast.LENGTH_SHORT).show()
             // update database
         }
    }
}

If it does not work, please post the stacktrace of the error you get this time

Upvotes: 0

Yashaswi N P
Yashaswi N P

Reputation: 933

In your RegistrationActivity class, you are trying to get the text of the edittext even before the view is initalized. You must call the editText.text.toString() only after the setContentView()

Follow the below code:

package com.example.myapplication

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_registration.*

class RegistrationActivity : AppCompatActivity() {


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

        val userName = etUserName.text.toString()
        val userEmail = etUserEmail.text.toString()
        val userPassword = etPassword.text.toString()

        btnRegistration.setOnClickListener {
            if (validate() == false)
                Toast.makeText(this, "Incomplete data", Toast.LENGTH_SHORT).show()
            else {
                Toast.makeText(this, "Registration successed", Toast.LENGTH_SHORT).show()
                // update database
            }
        }
    }

Upvotes: -1

Related Questions