0

I have a MapsActivity with HeatMap on it. Like Google Maps there is an option to filter data by various categories (like Satellite, Terrain etc in case of Google Maps).

I have a button which brings a modal bottom sheet. This bottom sheet contains a recycler view with a list of categories.

When I click on a category and run a method to make an ArrayList of new filtered data the size turns out to be 0.

Method in MapsActivity class

public void changeHeatMap(int category, int remove){
    int count = 0;
    if (remove==0) {
        ArrayList<LatLng> catHeatMap = new ArrayList<>(3);
        while (count < il_cat_data.size()) {
            if (il_cat_data.get(count) == category) {
                catHeatMap.add(list.get(count));
            }
            count++;
        }
        mProvider.setData(catHeatMap);
    }else{
        mProvider.setData(list);
    }
}

OnClick method in the RecyclerView Adapter class

public void onClick(View view) {
        int position = getAdapterPosition();
        MapsActivity mapsActivity = new MapsActivity();
        if (position==MapsActivity.isClicked){
            MapsActivity.isClicked = -1;
            mapsActivity.changeHeatMap(position,1);
        }else{
            MapsActivity.isClicked = position;
            mapsActivity.changeHeatMap(position,0);
        }
    }

It throws this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.maps.android.heatmaps.HeatmapTileProvider.setData(java.util.Collection)' on a null object reference

While loop is not executed at all in changeHeatMap method and if I put a breakpoint and check size of il_cat_data then it comes out to be 0.

I have checked that data is added in il_cat_data when the app is started. The 'list' is an arraylist which contains complete data and is used to set up the first heatmap.

What can be wrong here? Is data not accessible because of the modal bottom sheet.

user3884753
  • 255
  • 6
  • 16
  • `java.lang.NullPointerException` . And the culprit is `MapsActivity mapsActivity = new MapsActivity();` . You never create an Activity Object with `new` keyword . Read about [Activity](https://developer.android.com/reference/android/app/Activity). – ADM Apr 26 '18 at 15:31
  • 1
    Possible 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) – ADM Apr 26 '18 at 15:32

1 Answers1

0

This answer helped me.

In the MapsActivity created a variable

static MapsActivity mapsActivity;

In onCreate state:

MapsActivity = this;

Added this method

public static MapsActivity getInstance(){
    return   mapsActivity;
}

In the Recycler Adapter onClick method changed

MapsActivity mapsActivity = new MapsActivity();
mapsActivity.changeHeatMap(position,1);

to

MapsActivity.getInstance().changeHeatMap(position,1);
user3884753
  • 255
  • 6
  • 16