Getting NullPointerException On Location Android
<p>This is how I am trying to get location in my android app. But Each time I am starting my App it is giving me NullPointerException  though i checked if the location is null. What is happening here? Please help me.</p>

<pre><code>public Location getLocation () {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled &amp;&amp; !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    //updates will be send according to these arguments
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();


                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }
</code></pre>

<p>And this method running in a Service which I am calling by an intent.I am sending my Location to Server Using AsyncTask Like this: </p>

<pre><code>@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        Log.i(LOG, "Service started");
        Log.i("asd", "This is sparta");



      new SendToServer().execute(Double.toString(getLocation().getLongitude()),Double.toString(getLocation().getLatitude());
return START_STICKY;
    }
</code></pre>

<p>Here is my logcat:</p>

<pre><code>3-04 12:55:03.748    3235-3235/sourcemate.com.locationsender E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: sourcemate.com.locationsender, PID: 3235
    java.lang.RuntimeException: Unable to start service sourcemate.com.locationsender.LocationService@2f790e5a with Intent { cmp=sourcemate.com.locationsender/.LocationService }: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2914)
            at android.app.ActivityThread.access$2100(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
            at sourcemate.com.locationsender.LocationService.onStartCommand(LocationService.java:75)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2897)
            at android.app.ActivityThread.access$2100(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
</code></pre>