There is a code to add positions to the array and display the graph in GraphView:

for (i = 0; i < FragmentDiaryWeight.weightDiaryList.size(); i++) { if (FragmentDiaryWeight.weightDiaryList.get(i).getmMonth() == 3) { weightDiaryListSorted.add(new WeightDiaryClass( FragmentDiaryWeight.weightDiaryList.get(i).getmDate(), FragmentDiaryWeight.weightDiaryList.get(i).getmWeight(), FragmentDiaryWeight.weightDiaryList.get(i).getmImt(), FragmentDiaryWeight.weightDiaryList.get(i).getmDay(), FragmentDiaryWeight.weightDiaryList.get(i).getmMonth() )); } } dp = new DataPoint[i]; for (i = 0; i < weightDiaryListSorted.size(); i++) { dp[i] = new DataPoint(weightDiaryListSorted.get(i).getmDay(), weightDiaryListSorted.get(i).getmWeight()); } LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp); 

On the LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp); line LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp); crashes to null due to:

java.lang.NullPointerException: Attempt to invoke interface 'double com.jjoe64.graphview.series.DataPointInterface.getX ()' on a null object reference

I do not understand why, because dp contains positions with x and y coordinates.

How to fix?

    1 answer 1

    Solved. I used to determine the dimension of two arrays the same variable i . Adding another iSorted variable solved the problem. Ready code:

     public void onClick(View v) { for (iOlder = 0; iOlder < FragmentDiaryWeight.weightDiaryList.size(); iOlder++) { if (FragmentDiaryWeight.weightDiaryList.get(iOlder).getmMonth() == 3) { weightDiaryListSorted.add(new WeightDiaryClass( FragmentDiaryWeight.weightDiaryList.get(iOlder).getmDate(), FragmentDiaryWeight.weightDiaryList.get(iOlder).getmWeight(), FragmentDiaryWeight.weightDiaryList.get(iOlder).getmImt(), FragmentDiaryWeight.weightDiaryList.get(iOlder).getmDay(), FragmentDiaryWeight.weightDiaryList.get(iOlder).getmMonth() )); } } iSorted = weightDiaryListSorted.size(); dp = new DataPoint[iSorted]; for (iSorted = 0; iSorted < weightDiaryListSorted.size(); iSorted++) { dp[iSorted] = new DataPoint(weightDiaryListSorted.get(iSorted).getmDay(), weightDiaryListSorted.get(iSorted).getmWeight()); } LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);