1

I've initialized HashMap#onCreate method and i want to use HashMap in addLocation method. Everything looks okay but I don't understand why I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.put(java.lang.Object, java.lang.Object)' on a null object reference

when I call locationList#put method.

Here's my code:

public class InitLocationsOnStartup extends Application implements LocationListener {
    HashMap < String, Double[] > locationList;
    List < StationViewModel > stations;

    @Override
    public void onCreate() {
        super.onCreate();
        stations = new ArrayList < StationViewModel > ();
        locationList = new HashMap < > (); // 
    }

    public void getStations(List < StationViewModel > list) {
        this.stations = list;
        addLocation(locationList);
    }

    public HashMap < String, Double[] > addLocation(HashMap < String, Double[] > locationList) {
        for (int i = 0; i < stations.size(); i++) {
            latLong = new Double[] {
                Double.parseDouble(stations.get(i).getLatitude()),
                    Double.parseDouble(stations.get(i).getLongitude())
            };

            if (stations.get(i).getStationName() != null && latLong != null) {
                locationList.put(stations.get(i).getStationName(), latLong);

            }
        }
        return locationList;;
    }
Edric
  • 24,639
  • 13
  • 81
  • 91
more
  • 183
  • 1
  • 7
  • 18

1 Answers1

1

In this case the addLocation is being called before the onCreate() method is executed.

I guess you are accessing an instance of InitLocationsOnStartup which is not managed by the Android framework.

If you have code like:

InitLocationsOnStartup startup = new InitLocationsOnStartup();
startup.addLocation(..., ...);

You are going to get this NullPointerException.

The best way to access this class should be in Android activities using the

getApplication()

method. You could create a static factory method to access this object in a safe way, like e.g:

public static InitLocationsOnStartup getInitLocationsOnStartup(Activity activity) {
    return (InitLocationsOnStartup) activity.getApplication();
}

This should get you the right instance of the InitLocationsOnStartup class which has been correctly initialized by Android.

This method could then be used from Android activities like this:

InitLocationsOnStartup startup = getInitLocationsOnStartup(this);
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • Thanks for your answer.I want use in Runnable class.So what i need change this code? Maybe like getInitLocationsOnStartup(Runnable runnable) – more Apr 28 '18 at 11:11
  • @more You will always need to have a reference to an `android.app.Activity` or `android.app.Service` to be able to call the `getApplication()` method. `Runnable` will not help you, because it is neither an Activity or Service via which you can get the right instance of `InitLocationsOnStartup`. Yet, if the Runnable you implement has a reference to an `android.app.Activity` or `android.app.Service` then you can use that reference to call `getApplication()`. – gil.fernandes Apr 28 '18 at 11:18
  • @more in other words: make sure that the `Runnable` object you create has a reference to an Android activity or Android service, so that you can call the `getApplication()` method from either of these. – gil.fernandes Apr 28 '18 at 11:19
  • Thanks for everything! – more Apr 28 '18 at 11:45
  • @more You are most welcome :) – gil.fernandes Apr 28 '18 at 11:50