Im coding a custom adapter for listview extends from base adapter. Basically Its will list some name, surname etc.
getSize method In adapter class, getting nullpointException. I cant figure out whats happen.
2021-11-27 13:55:36.561 27997-27997/com.example.listviewlesson E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.listviewlesson, PID: 27997 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listviewlesson/com.example.listviewlesson.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) 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:858) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.example.listviewlesson.UserListAdapter.getCount(UserListAdapter.java:27) at android.widget.ListView.setAdapter(ListView.java:575) at com.example.listviewlesson.MainActivity.fillList(MainActivity.java:26) at com.example.listviewlesson.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:7136) ...
This is my adapter class
package com.example.listviewlesson;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class UserListAdapter extends BaseAdapter {
Context context;
List<UserModel> uList;
public UserListAdapter(List<UserModel> list, Context context) {
this.uList = list;
this.context = context;
}
@Override
public int getCount() {
return uList.size();
}
@Override
public Object getItem(int position) {
return uList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = LayoutInflater.from(context).inflate(R.layout.layout, parent, false);
TextView name = (TextView) layout.findViewById(R.id.namexml);
TextView surname = (TextView) layout.findViewById(R.id.surnamexml);
TextView age = (TextView) layout.findViewById(R.id.agexml);
TextView team = (TextView) layout.findViewById(R.id.teamxml);
name.setText(uList.get(position).getName());
surname.setText(uList.get(position).getSurname());
age.setText(uList.get(position).getAge());
team.setText(uList.get(position).getTeam());
return layout;
}
}
And this is main activity
package com.example.listviewlesson;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<UserModel> userList;
UserListAdapter adp;
ListView lview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tanimla();
fillList();
}
public void fillList() {
userList = new ArrayList<UserModel>(userList);
UserModel model0 = new UserModel("USER1", "SRNAME", "29", "BJK");
UserModel model1 = new UserModel("USER2", "SRNAME", "29", "BJK");
UserModel model2 = new UserModel("USER3", "SRNAME", "1", "BJK");
userList.add(model0);
userList.add(model1);
userList.add(model2);
adp = new UserListAdapter(userList, this);
lview.setAdapter(adp);
}
void Tanimla() {
lview = (ListView) findViewById(R.id.listview0);
}
}
This is main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/listview0"
/>
CodePudding user response:
Everything is okay just change this line of code in fillList() method.
public void fillList() {
userList = new ArrayList<>();
UserModel model0 = new UserModel("USER1", "SRNAME", "29", "BJK");
UserModel model1 = new UserModel("USER2", "SRNAME", "29", "BJK");
UserModel model2 = new UserModel("USER3", "SRNAME", "1", "BJK");
userList.add(model0);
userList.add(model1);
userList.add(model2);
adp = new UserListAdapter(userList, this);
lview.setAdapter(adp);
}