Search code examples
javaandroidandroid-studionullpointerexception

How to solve NullPointerException in the following code?


[This is the code where the exception occurred.

package com.example.shrine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    final TextInputLayout password1 = findViewById(R.id.password_text_input);
    final TextInputEditText password = findViewById(R.id.password_edit_text);
    Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ctx=this;
    MaterialButton Next=findViewById(R.id.next_button);
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ctx,"This can't be happening!",Toast.LENGTH_SHORT).show();
            Intent products = new Intent(MainActivity.this, products_act.class);
            startActivity(products);

        }
    });


}

}

package com.example.shrine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class products_act extends AppCompatActivity {
    Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context=this;
    setContentView(R.layout.activity_products_act);
    Button back=findViewById(R.id.button);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "Back Button Clicked!!", Toast.LENGTH_SHORT).show();
            Intent backToHome = new Intent(products_act.this, MainActivity.class);
            startActivity(backToHome);

        }
    });}}

ERROR:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shrine, PID: 18243
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shrine/com.example.shrine.MainActivity}: 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:2718)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 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:152)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
    at android.content.Context.obtainStyledAttributes(Context.java:655)
    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.shrine.MainActivity.<init>(MainActivity.java:18)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Please look into this problem of NullPointerException as I am new to Android development and still Learning therefore please help.The attached photos [1] and [2] are the code snaps. Please check them out. P.S.-I have removedthe code as images.Thankyou for the suggestion


Solution

  • at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
    at com.example.shrine.MainActivity.<init>(MainActivity.java:18)
    

    This tells you're calling findViewById() too early in MainActivity's init phase e.g. when initialising its fields. Move the findViewById() calls to onCreate() after setContentView().