MainActivity of my code
package com.example.recycleview;
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.view.View;
import android.widget.Adapter;
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>arrContact= new ArrayList<>();
RecyclerContactAdapter adapter;
RecyclerView recyclerView;
FloatingActionButton btnOpndlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= findViewById(R.id.recycleContact);
btnOpndlg = findViewById(R.id.btnopndlg);
btnOpndlg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.add_update);
EditText edtname= dialog.findViewById(R.id.edtname);
EditText edtno= dialog.findViewById(R.id.edtno);
Button btnActn= dialog.findViewById(R.id.addbtn);
btnActn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name=" ", number="";
if(!edtname.getText().toString().equals("")){
name= edtname.getText().toString();
}
else{
Toast.makeText(MainActivity.this, "Please Enter the name ", Toast.LENGTH_SHORT).show();
}
if(!edtno.getText().toString().equals("")){
number= edtno.getText().toString();
}
else{
Toast.makeText(MainActivity.this, "Please Enter the number ", Toast.LENGTH_SHORT).show();
}
arrContact.add(new ContactModel(name,number));
adapter.notifyItemInserted(arrContact.size()-1);
recyclerView.scrollToPosition(arrContact.size()-1);
dialog.dismiss();
}
});
dialog.show();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
arrContact.add(new ContactModel(R.drawable.ap,"Shank","908784836"));
arrContact.add(new ContactModel(R.drawable.lp,"Luffy","908784836"));
arrContact.add(new ContactModel(R.drawable.kl,"Aurther","908784836"));
arrContact.add(new ContactModel(R.drawable.ss,"madara","908784836"));
arrContact.add(new ContactModel(R.drawable.gp,"mob","908784836"));
arrContact.add(new ContactModel(R.drawable.hp,"Sakuna","908784836"));
arrContact.add(new ContactModel(R.drawable.gp,"mob","908784836"));
arrContact.add(new ContactModel(R.drawable.ap,"Shank","908784836"));
arrContact.add(new ContactModel(R.drawable.lp,"luffy","908784836"));
arrContact.add(new ContactModel(R.drawable.ss,"madra","908784836"));
arrContact.add(new ContactModel(R.drawable.ss,"madra","908784836"));
arrContact.add(new ContactModel(R.drawable.ap,"shank","908784836"));
arrContact.add(new ContactModel(R.drawable.gp,"mob","908784836"));
RecyclerContactAdapter adapter= new RecyclerContactAdapter(this, arrContact);
recyclerView.setAdapter(adapter);
}
}
FATAL EXCEPTION: main Process: com.example.recycleview, PID: 2731 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.recycleview.RecyclerContactAdapter.notifyItemInserted(int)' on a null object reference at com.example.recycleview.MainActivity$1$1.onClick(MainActivity.java:59) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
please check the above code mainacitvity
CodePudding user response:
So at the very beginning of your code, you have declared a variable:
public class MainActivity extends AppCompatActivity {
ArrayList<ContactModel>arrContact= new ArrayList<>();
RecyclerContactAdapter adapter; // You have declared an adapter here
RecyclerView recyclerView;
FloatingActionButton btnOpndlg;
...
}
But close to the end of your code, you have defined another adapter
:
RecyclerContactAdapter adapter= new RecyclerContactAdapter(this, arrContact);
recyclerView.setAdapter(adapter);
The above will only create a new local variable and keeps your top adapter
being null
.
So if you would like to access adapter
in any function within the same class, you should assign to your top declared adapter
. And therefore you should remove RecyclerContactAdapter
for the bottom one:
adapter = new RecyclerContactAdapter(this, arrContact);
recyclerView.setAdapter(adapter);
CodePudding user response:
adapter don't assign obj,you need Assigns a value to a declared variable.
adapter = new RecyclerContactAdapter(this, arrContact);
CodePudding user response:
You can also add a check, if it's null or not
if(adapter !=null) {
adapter.notifyItemInserted(arrContact.size()-1);
}