Skip to content

Snappy1

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

[FIXED] java – FATAL EXCEPITON , android studio keeps crashing For a button ;

Posted on November 11, 2022 By

Solution 1 :

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 the dreadful 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

Solution 2 :

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);
 

Problem :

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??? 

READ  [FIXED] How to convert an open source android studio project into a dependency for another?
Powered by Inline Related Posts
Android Tags:android, java, xml

Post navigation

Previous Post: What is the 21 day fitness challenge?
Next Post: [FIXED] android – Waiting for coroutine finish before fragment close

Related Posts

[FIXED] java – Unable to access drawables from MapActivity Android
[FIXED] java – How to properly set layout visibility from another activity? Android
[FIXED] How to install tfs in android studio? Android
[FIXED] android – How to show Firebase data only once? Android
[FIXED] java – Android keep button or view in front of all activities within the app without using baseActivity Android
[FIXED] xamarin.android – Twilio Conversation on Xamarin, send Message 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