java – Caling a clickListener inside a clickListener return error null object reference error

I’m getting the error [Attempt to invoke virtual method ‘void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference] on running the below code and from Logcat I checked the error is at line when calling the setOnClickListener() on the button id “problmBtn” at line 47


    package com.royslab.recview;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<ContactModel> list = new ArrayList<>();
    RecyclerView recyclerView;
    ContactRecyclerAdapter adapter;
    FloatingActionButton btnOpenDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        btnOpenDialog = findViewById(R.id.btnOpenDialog);

        btnOpenDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.update_contact_dialog_layout);

                EditText addTxtName = findViewById(R.id.addTxtName);
                EditText addTxtNumber = findViewById(R.id.addTxtNumber);
                Button btnAction = findViewById(R.id.problmBtn);

                btnAction.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                            String name = "", number = "";
                            if (!addTxtName.getText().toString().equals("")){
                                name = addTxtName.getText().toString();
                            } else{
                                Toast.makeText(MainActivity.this, "Please add a valid name", Toast.LENGTH_SHORT).show();
                            }
                            if (!addTxtNumber.getText().toString().equals("")){
                                number = addTxtNumber.getText().toString();
                            } else {
                                Toast.makeText(MainActivity.this, "Please add valid contact number", Toast.LENGTH_SHORT).show();
                            }
                            list.add(new ContactModel(name, number));
                            adapter.notifyItemInserted(list.size() - 1);
                            recyclerView.scrollToPosition(list.size() - 1);

                    }
                });
                dialog.show();
            }
        });




        list.add(new ContactModel(R.drawable.a, "Kalyan","9999999999"));
        list.add(new ContactModel(R.drawable.b, "Uttam","8888888888"));
        list.add(new ContactModel(R.drawable.c, "Bulu","99999999999"));
        list.add(new ContactModel(R.drawable.d, "Pampa","00000000000"));
        list.add(new ContactModel(R.drawable.a, "Kalyan","9999999999"));
        list.add(new ContactModel(R.drawable.b, "Uttam","8888888888"));
        list.add(new ContactModel(R.drawable.c, "Bulu","99999999999"));
        list.add(new ContactModel(R.drawable.d, "Pampa","00000000000"));
        


        adapter = new ContactRecyclerAdapter(this, list);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
}


The XML code is as follows-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:background="@color/teal_700">
    <TextView
        android:layout_width="270dp"
        android:layout_height="3dp"
        android:background="@color/black"
        android:layout_gravity="center"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/add_new_contact"
        android:gravity="center"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="@color/white"/>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="7dp"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@color/teal_200">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:id="@+id/llRow"
            android:padding="11dp">
            <androidx.cardview.widget.CardView
                android:layout_width="100dp"
                android:layout_height="100dp"
                app:cardCornerRadius="50dp">
                <ImageView
                    android:id="@+id/addImgContact"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:contentDescription="@string/contact_image"
                    android:src="@drawable/ic_user"
                    android:background="@color/white"
                    />
            </androidx.cardview.widget.CardView>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="11dp"
                android:orientation="vertical"
                tools:ignore="UselessLeaf"
                android:background="@color/white">
                <EditText
                    android:id="@+id/addTxtName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/contact"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:textColor="@color/material_dynamic_primary40"
                    android:textColorHint="@color/black"
                    android:layout_marginBottom="3dp"
                    android:background="@color/white"
                    android:inputType="text"
                    android:padding="3dp"
                    android:autofillHints="name" />
                <EditText
                    android:id="@+id/addTxtNumber"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/contact_number"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:padding="3dp"
                    android:background="@color/material_dynamic_neutral30"
                    android:inputType="number"
                    android:autofillHints="phone" />

            </LinearLayout>
        </LinearLayout>

    </androidx.cardview.widget.CardView>

    <Button
        android:id="@+id/problmBtn"
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/add_new_contact"
        android:textStyle="bold"
        android:backgroundTint="@color/material_dynamic_primary20"
        android:layout_marginBottom="10dp"
        />
    <TextView
        android:layout_width="310dp"
        android:layout_height="3dp"
        android:background="@color/white"
        android:layout_gravity="center"/>
</LinearLayout>

Leave a Comment