0

I'm currently working on a task reminder app that makes use of a custom calendar where reminders (i.e tasks) are stored. But for some reason, this error pops up...

    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on 
     a null object reference
        at com.example.projecttweeta.MyGridAdapter.getView(MyGridAdapter.java:78)
        at android.widget.AbsListView.obtainView(AbsListView.java:2387)
        at android.widget.GridView.onMeasure(GridView.java:1085)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1204)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:723)
        at android.view.View.measure(View.java:25466)
        at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:792)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:480)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:134)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:277)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:119)
        at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1578)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1690)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:490)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        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)

Its coming from this particular Java class...

package com.example.projecttweeta;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MyGridAdapter extends ArrayAdapter {
    List<Date> dates;
    Calendar currentDate;
    List<Tasks> tasks;
    LayoutInflater inflater;

    public MyGridAdapter(@NonNull Context context, List<Date> dates, Calendar currentDate, List<Tasks> tasks) {
        super(context, R.layout.single_cell_layout); // work on that after this...

        this.dates = dates;
        this.currentDate = currentDate;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dates.size();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Date monthDate = dates.get(position);
        Calendar dateCalender = Calendar.getInstance();
        dateCalender.setTime(monthDate);
        int dayNo = dateCalender.get(Calendar.DAY_OF_MONTH);
        int displayMonth = dateCalender.get(Calendar.MONTH) + 1;
        int displayYear = dateCalender.get(Calendar.YEAR);
        int currentMonth = currentDate.get(Calendar.MONTH) + 1;
        int currentYear = currentDate.get(Calendar.YEAR);

        View view = convertView;

        if (view == null) {
            view = inflater.inflate(R.layout.single_cell_layout, parent, false);
        }

        if (displayMonth == currentMonth && displayYear == currentYear) {
     //            view.setBackgroundColor(getContext().getResources().getColor(R.color.color_primary));
            view.setBackgroundColor(Color.parseColor("#60DDED"));


        } else {
    //            view.setBackgroundColor(Color.parseColor("#D3D3D3"));
                  view.setBackgroundColor(Color.TRANSPARENT);


        }

        TextView day_number = view.findViewById(R.id.calender_day);
        TextView task_number = view.findViewById(R.id.task_id);
        day_number.setText(String.valueOf(dayNo));
        Calendar taskCalender = Calendar.getInstance();
        ArrayList<String> arrayList = new ArrayList<>();


        for (int i = 0; 1 < tasks.size(); i++ ) {
            taskCalender.setTime(convertStringToDate(tasks.get(i).getDATE()));
            if (dayNo == taskCalender.get(Calendar.DAY_OF_MONTH) && displayMonth == taskCalender.get(Calendar.MONTH) + 1
                    && displayYear == taskCalender.get(Calendar.YEAR)) {
                arrayList.add(tasks.get(i).getTITLE());
                task_number.setText(arrayList.size() + " Tasks");
            }
        }


        return view;
    }


    private Date convertStringToDate(String taskDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd", Locale.ENGLISH);
        Date date = null;
        try {
            date = format.parse(taskDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    @Override
    public int getPosition(@Nullable Object item) {
        return dates.indexOf(item);
    }


    @Nullable
    @Override
    public Object getItem(int position) {
        return dates.get(position);
    }
    }

Please I need a response as soon as possible thanks. If you need the remaining codes, that would be fine.

JahsOwn
  • 1
  • 2
  • It looks like `tasks` is null. – John Gordon Feb 08 '21 at 17:51
  • There are two problems in line 78 in the `getView()` method: `for (int i = 0; 1 < tasks.size(); i++ ) `. Problem #1 and the reason for your `NullPointerException` is, that you never assign anything to your member variable `tasks`. Without judging the rest of the code, your intention was probably to pass it on in the constructor like this: `this.tasks = tasks;` After you have done that, you will run into Problem #2, an `IndexOutOfBoundsException` because you are checking for `1 < tasks.size()` instead of `i < tasks.size()` in the for loop. – c0delama Feb 08 '21 at 17:56
  • So please is there any way around these errors? – JahsOwn Feb 08 '21 at 18:06
  • So Ive been able to tackle problem #1 but Im stuck at problem #2. Is there any way to fix this? – JahsOwn Feb 08 '21 at 22:18
  • Ah thanks a lot guys!! I found the errors a while ago and its running properly. Kudos to you!! – JahsOwn Feb 09 '21 at 18:33

0 Answers0