NullPointerException in Google Maps Fragmentactivity : <p>I want to show an alertbox in my MapsActivity, if I enter a geofence. I can detect the "enter", and i can also trigger a notification, but when i want to create an alert box I get this error: </p>

<pre><code>    01-15 20:10:08.443 17477-17797/com.example.labsw.bugavo E/AndroidRuntime: FATAL EXCEPTION: IntentService[GeofenceReceiver]
                                                                      Process: com.example.labsw.bugavo, PID: 17477
                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                          at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
                                                                          at com.example.labsw.bugavo.navigation.MapsActivity.triggerAlertBoxOnFenceEntry(MapsActivity.java:669)
                                                                          at com.example.labsw.bugavo.navigation.geofencing.GeofenceReceiver.onHandleIntent(GeofenceReceiver.java:95)
                                                                          at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.os.HandlerThread.run(HandlerThread.java:61)
</code></pre>

<p>It seems like i dont get the right context, but i tried </p>

<pre><code>      AlertDialog.Builder alertbox = new AlertDialog.Builder(getApplicationContext());
</code></pre>

<p>and </p>

<pre><code>     AlertDialog.Builder alertbox = new AlertDialog.Builder(getBaseContext);
</code></pre>

<p>also, but nothing works. </p>

<p>Where I detect the "enter" and trigger the method for the alertbox (GeofenceReciever.java):</p>

<pre><code>public GeofenceReceiver() {
    super("GeofenceReceiver");
}
MapsActivity mapsActivity = new MapsActivity();

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "onHandleIntent: GeofenceReceiver called");
    GeofencingEvent geoEvent = GeofencingEvent.fromIntent(intent);
    if (geoEvent.hasError()) {
        Log.i(TAG, "Error GeofenceReceiver.onHandleIntent");
    } else {
        Log.i(TAG, "GeofenceReceiver : Transition -&gt; "
                + geoEvent.getGeofenceTransition());

        int transitionType = geoEvent.getGeofenceTransition();
        geofenceId = 0;

        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
            List&lt;Geofence&gt; triggerList = geoEvent.getTriggeringGeofences();

            for (Geofence geofence : triggerList) {
                Log.i(TAG, "onHandleIntent: geofence id = " + geofence.getRequestId());
                CustomGeofence sg = GeofenceAndStationsStore.getInstance()
                        .getSimpleGeofences().get(geofence.getRequestId());

                geofenceId = sg.getStationId();


                String transitionName = "";
                switch (transitionType) {

                    case Geofence.GEOFENCE_TRANSITION_ENTER:
                        transitionName = "enter";
                        break;

                }
                String date = DateFormat.format("yyyy-MM-dd hh:mm:ss",
                        new Date()).toString();

                GeofenceNotification geofenceNotification = new GeofenceNotification(
                        this);
                geofenceNotification
                        .displayNotification(sg, transitionType);
            }
        }


        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {


            Intent newintent = new Intent(this, MapsActivity.class);

            arrivedAtStation = true;

            if(geofenceId != 0) {
                newintent.putExtra("stationId", geofenceId);

            } else {
                Log.i(TAG, "onHandleIntent: irgendwas stimmt nicht.. keine stationId von geofence vorhanden");
            }
            newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(newintent);

            Log.i(TAG, "onHandleIntent: TRIGGER ALERT DIALOG");
            mapsActivity.triggerAlertBoxOnFenceEntry();
        }

    }
}
</code></pre>

<p>}</p>

<p>and where i want to create the alertbox (MapsActivity.java):</p>

<pre><code>public void triggerAlertBoxOnFenceEntry() {

    AlertDialog.Builder alertbox = new AlertDialog.Builder(MapsActivity.this);

    alertbox.setTitle("Station Erreicht!");
    alertbox.setMessage("Sie haben die Station " + GeofenceReceiver.geofenceId + " erreicht! \nWollen Sie Die Fragerunde starten?\"");
    alertbox.setPositiveButton("Ja", null);
    alertbox.setNegativeButton("Nein", null);
    alertbox.create();
    alertbox.show();

    System.out.println("Sie haben die Station " + GeofenceReceiver.geofenceId + " erreicht! \nWollen Sie Die Fragerunde starten?");
    GeofenceReceiver.arrivedAtStation = false;

}
</code></pre>

<p>I also want to start a new Activity out of that alertbox (which doesn´t work either, i guess its because of the wrong context too) 
It would be awesome if you guys could help me out. </p>
