-1

Hello my problem is that i am getting a null pointer exception in my program i am getting a exception in a imageview of a layout file i am trying to make a firebase app so i use try-catch blocks for handling firebase error so i decided to make a beautiful dialog box but when i test it first time it runs but now it throws a null pointer exception so please help me here is my code and activities

:- my register activity

public class registerActivity extends Activity {
Dialog error_dialog_box_register;
    TextView dialog_title_register, dialog_message_register;
    ImageView dialog_main_image, dialog_cancel_image;
    Button ok_button, login_button;
    private Button RegisterButton;

@Override
    protected void onCreate(Bundle savedInstanceState) {
RegisterButton = findViewById(R.id.register_in_button);
RegisterButtonListener();
}
private void RegisterButtonListener() {
        RegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CreateNewAccount();

            }
        });
    }

 private void CreateNewAccount() {
mAuth.createUserWithEmailAndPassword(Email, Password)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            if (task.isSuccessful()) {
                                Register_Progress_bar.setVisibility(View.GONE);
                                SendUserToDate_of_birthActivity();
                            } else {
                                try {
                                    Register_Progress_bar.setVisibility(View.GONE);
                                    throw Objects.requireNonNull(task.getException());
                                } catch (FirebaseAuthUserCollisionException f) {
                                    ShowErrorPopup();
                                    Toast.makeText(registerActivity.this, "User already exists", Toast.LENGTH_SHORT).show();
                                } catch (FirebaseNetworkException e) {
                                    Toast.makeText(registerActivity.this, "Error network error", Toast.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    });
}
    @SuppressLint("SetTextI18n")
    private void ShowErrorPopup() {


        error_dialog_box_register.setContentView(R.layout.custom_popup_p);

        dialog_cancel_image = (ImageView) findViewById(R.id.Cancel_dialog_box_register);
        ok_button = (Button) findViewById(R.id.dialog_ok_button_register);
        login_button = (Button) findViewById(R.id.dialog_login_button_register);
        dialog_title_register = (TextView) findViewById(R.id.Dialog_title_register);
        dialog_message_register = (TextView) findViewById(R.id.dialog_message_register);

        dialog_cancel_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                error_dialog_box_register.dismiss();
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SendUserToLoginActivity();
            }
        });

        dialog_title_register.setText("Error");
        dialog_message_register.setText("Email already exists please login or try again");

        error_dialog_box_register.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        error_dialog_box_register.show();




    }

}

Here is my xml file:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/Cancel_dialog_box_register"
        android:layout_width="46dp"
        android:layout_height="38dp"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="7dp"
        android:layout_marginEnd="7dp"
        android:elevation="5dp"
        android:src="@drawable/ic_close_black_24dp"
        />


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        app:cardBackgroundColor="@color/ColorRed"
        app:cardCornerRadius="15dp"

        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="25dp"
                android:orientation="vertical">


                <TextView
                    android:id="@+id/Dialog_title_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:text=""
                    android:textAlignment="center"
                    android:textColor="#000000"
                    android:textSize="25sp"
                    android:textStyle="bold" />

                <ImageView
                    android:id="@+id/dialog_main_image_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginBottom="10dp"
                    android:src="@drawable/ic_error_black_24dp" />

                <TextView
                    android:id="@+id/dialog_message_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginBottom="20dp"
                    android:text=""
                    android:textAlignment="center"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/dialog_login_button_register"
                        android:layout_width="180dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start"
                        android:layout_marginStart="20dp"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/login_b_custom"
                        android:text="@string/login"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="15sp" />

                    <Button
                        android:id="@+id/dialog_ok_button_register"
                        android:layout_width="180dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:layout_marginStart="15dp"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/login_b_custom"
                        android:text="Ok"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="15sp"

                        />
                </LinearLayout>


            </LinearLayout>


        </LinearLayout>


    </androidx.cardview.widget.CardView>


</RelativeLayout>

And i am getting error like these

2019-08-03 15:52:13.840 5711-5711/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-08-03 15:52:13.841 5711-5711/? E/Zygote: accessInfo : 1
2019-08-03 15:52:43.642 5711-5711/com.geetmp3.themusicapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.geetmp3.themusicapp, PID: 5711
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.geetmp3.themusicapp.AccountCreation.registerActivity.ShowErrorPopup(registerActivity.java:193)
        at com.geetmp3.themusicapp.AccountCreation.registerActivity.access$300(registerActivity.java:34)
        at com.geetmp3.themusicapp.AccountCreation.registerActivity$2.onComplete(registerActivity.java:146)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7076)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

Please reply me why i am getting null pointer exception and please help me

2 Answers2

0

In your OnCreate function you assign your RegisterButton as:

RegisterButton = findViewById(R.id.register_in_button);

but in your layout you have two buttons with the IDs of:

android:id="@+id/dialog_login_button_register"

and

android:id="@+id/dialog_ok_button_register"

So your findViewById call returns with null because no button with the ID of register_in_button exists. Then you get an exception when you attempt to invoke the null object's method.

pappbence96
  • 1,164
  • 2
  • 12
  • 20
0

Please create variables reference by dialog because all ImageVeiw or textview are inside the dialog

    error_dialog_box_register= new Dialog(this);
    error_dialog_box_register.requestWindowFeature(Window.FEATURE_NO_TITLE);
    error_dialog_box_register.setContentView(R.layout.custom_popup_p);
    error_dialog_box_register.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    error_dialog_box_register.setCancelable(false);

    dialog_cancel_image = error_dialog_box_register.findViewById(R.id.Cancel_dialog_box_register);
Divyanshu
  • 462
  • 6
  • 17
  • can you explain me properly please ? – Hunny Arora Aug 03 '19 at 17:39
  • If you are using An Imageview in activity then you can create it's reference directly by findViwById() and if any layout appear on the parent layout and contain Many views then first create you Dialog reference then view reference by dialog. – Divyanshu Aug 05 '19 at 04:46