0

I got an error on my list adapter listAdapter = new ListAdapter(models, this);, I'm making myself a custom adapter name ListAdapter.

I thought because there is no data so I put in the data using model.add and after I tried to put in the data programmatically, I still got an error says the same thing.

How can I solve this? Below I give you some code examples.

MainActivity:

private ListAdapter listAdapter;
private ArrayList<Model> models;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listAdapter.notifyDataSetChanged();
    listView.setAdapter(listAdapter);
    findView();
    initView();
    initListener();
}

@Override
public void findView() {
    listView = findViewById(R.id.list_pemasukan);
    dateSet = findViewById(R.id.date_set);
    bAddIncome = findViewById(R.id.add_income);
    frameLayout = findViewById(R.id.frame_layout_income);
    bPickDate = findViewById(R.id.pick_date);
}

It said my adapter is null and I don't understand where it sets this value. I try to put some data on model using model.add but it still didn't change.

ListAdapter:

public class ListAdapter extends ArrayAdapter<Model> {

    private ArrayList<Model> dataTable;
    Context context;

    private static class ViewHolder {
        TextView txInOut;
        TextView txDay;
        TextView txMonth;
        TextView txYear;
        TextView Descriptions;
    }

    public ListAdapter(ArrayList<Model> data, Context context) {
        super(context, R.layout.list_row, data);
        this.context = context;
        this.dataTable = data;
    }

    private int lastPosition = -1;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Model dataModel = getItem(position);
        ViewHolder viewHolder;
        final View result;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.txInOut = convertView.findViewById(R.id.pemasukan);
            viewHolder.txDay = convertView.findViewById(R.id.tanggal);
            viewHolder.txMonth = convertView.findViewById(R.id.bulan);
            viewHolder.txYear = convertView.findViewById(R.id.year);
            viewHolder.Descriptions = convertView.findViewById(R.id.keterangan);

            result = convertView;

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result = convertView;
        }

        Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
        result.startAnimation(animation);
        lastPosition = position;

        viewHolder.txInOut.setText(dataModel.getPemasukan());
        viewHolder.txDay.setText(dataModel.getDay());
        viewHolder.txMonth.setText(dataModel.getMonth());
        viewHolder.txYear.setText(dataModel.getYear());
        viewHolder.Descriptions.setText(dataModel.getKeterangan());

        return convertView;
    }
}

This is my error, I don't understand where my error comes from.

Error message:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.educastudio.marbelmoneymanager.IncomeActivity.onCreate(IncomeActivity.java:53)
    at android.app.Activity.performCreate(Activity.java:6267)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5420) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-19 19:42:11.854 6740-6740/com.educastudio.marbelmoneymanager I/Process: Sending signal. PID: 6740 SIG: 9
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
Appem
  • 295
  • 1
  • 3
  • 16

5 Answers5

2

You should init listview before using it.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
    findView();
    initView();
    initListener();
    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listAdapter.notifyDataSetChanged();
    listView.setAdapter(listAdapter);
}
LiuWenbin_NO.
  • 1,216
  • 1
  • 16
  • 25
  • 1
    the question is a duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Please avoid answering duplicate questions. Mark them as duplicates instead – Vladyslav Matviienko Jul 19 '19 at 12:37
0

Your listView isn't initialized.

add findView above the listView.setAdapter(listAdapter); line.

For more info about NullPointException, and why it happens, click here

isaaaaame
  • 460
  • 4
  • 9
  • 1
    the question is a duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Please avoid answering duplicate questions. Mark them as duplicates instead – Vladyslav Matviienko Jul 19 '19 at 12:38
0

You don't need to call notifyDataSetChanged for the first time. Also you need to initialize your views by findViewById before accessing them:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
    findView();
    initView();
    initListener();

    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listView.setAdapter(listAdapter);

}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
  • the question is a duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Please avoid answering duplicate questions. Mark them as duplicates instead – Gourav Jul 19 '19 at 14:18
0

You just need to init your views before assign the adapter to list view else there is nothing wrong with your code mate.

In onCreateMethod -

findView();
initView();
initListener();
models = new ArrayList<>();
listAdapter = new ListAdapter(models, this);
listAdapter.notifyDataSetChanged();
listView.setAdapter(listAdapter);
Abhishek
  • 329
  • 3
  • 12
  • the question is a duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Please avoid answering duplicate questions. Mark them as duplicates instead – Gourav Jul 19 '19 at 14:17
-2
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
     findView();
    initView();
    initListener();
    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listAdapter.notifyDataSetChanged();
    listView.setAdapter(listAdapter);

}
Hardik Bambhania
  • 1,732
  • 15
  • 25
  • 1
    the question is a duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Please avoid answering duplicate questions. Mark them as duplicates instead – Vladyslav Matviienko Jul 19 '19 at 12:38
  • @VladyslavMatviienko : This is a platform where you can provide solution, Not to read document. – Hardik Bambhania Jul 19 '19 at 12:40
  • 1
    providing duplicate answers to duplicate questions harms this platform. The answers to same questions should not be fragmented and fractured between multiple questions. Therefore duplicate questions are not welcome, as well as answering them. – Vladyslav Matviienko Jul 19 '19 at 12:43