Home > Net >  FATAL EXCEPITON , android studio keeps crashing For a button ;
FATAL EXCEPITON , android studio keeps crashing For a button ;

Time:10-23

i have a button variable and is keeps crashing

here's MainActivity.java:

package com.example.datasaving;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Button button = this.findViewById(R.id.SAVE);
        TextView InputID = findViewById(R.id.textView);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = InputID.getText().toString();
                Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

and Here's 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/SAVE"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/save"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enter_your_gmail"
        android:textColorHint="#757575"
        app:layout_constraintBottom_toTopOf="@ id/SAVE"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

now it gives me an error saying :

FATAL EXCEPTION: main
                                                                                                    Process: com.example.datasaving, PID: 6961
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datasaving/com.example.datasaving.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3921)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4078)
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:233)
                                                                                                        at android.os.Looper.loop(Looper.java:334)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8348)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)
                                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                                        at com.example.datasaving.MainActivity.onCreate(MainActivity.java:20)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8363)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8341)
                                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3894)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4078) 
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                        at android.os.Looper.loopOnce(Looper.java:233) 
                                                                                                        at android.os.Looper.loop(Looper.java:334) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8348) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)

so this says that i have a null object but when i ctrl click that object it sends me back to The button what could be wrong am I doing something wrong or is it just the android studio??? 

CodePudding user response:

Instead of this,

Button button = this.findViewById(R.id.SAVE);
TextView InputID = findViewById(R.id.textView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
           

please do this.

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Button button = this.findViewById(R.id.SAVE);
 TextView InputID = findViewById(R.id.textView);

In your code, you are setting R.id.textView and R.id.SAVE when your activity hasn't yet set its layout via setContentView, so when you call

button.setOnClickListener{...}

it will crash because there is no button reference yet and because there is no layout yet being set, hence NullPointerException

For now, keep these 4 lines in mind and in-order.

  • onCreate(Bundle savedInstanceState)
  • super.onCreate(savedInstanceState)
  • setContentView(R.layout.my_layout)
  • findViewByid(R.id.my_view)

And to help you out further a little bit, when you encounter NullPointerException or the so called NPE, don't panic, just look at the left side of the . operator and try to think why its non-existent when you are calling some of its methods

CodePudding user response:

Put your initialization after

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

i.e.

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Button button = this.findViewById(R.id.SAVE);
 TextView InputID = findViewById(R.id.textView);
 
  • Related