Search code examples
androidgpsnullpointerexceptionpostdata

android null pointer exceptions in AsyncTask


I'm just starting to write under the android and ran into the problem that I do not know how to solve

PostData.java

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;

import android.os.AsyncTask;
import android.text.format.Time;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

class PostData extends AsyncTask<Void, Void, Void> {
    // GPSTracker class
        GPSTracker gps;
    //определяем переменную главного активити
        MainActivity ma;
        Teleport_user_profile_activity UP;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            // do stuff before posting data
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            postData();
            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            // do stuff after posting data
            super.onPostExecute(result);
        }



        public void postData() {
            // TODO Auto-generated method stub
            // Create a new HttpClient and Post Header
            //переводим значение double в стринг
            double latitudep = gps.getLatitude();
            double longitudep = gps.getLongitude();
        double totalLatitude = latitudep;
            String stotalLatitude = String.valueOf(totalLatitude);
            double totalLongitude = longitudep;
            String stotalLongitude = String.valueOf(totalLongitude);
            // временная переменная для определения времени устройства
            Time nowTime = new Time();
            nowTime.setToNow();
            String snowTime = String.valueOf(nowTime);
            //берем информацию о юзере
                UP.LoadUserInfoFromFile();
                UP.LoadUserInfopassFromFile();
                String UserInfo = Teleport_user_profile_activity.UserLoginFile;
                String UserPass = Teleport_user_profile_activity.UserPassFile;
            //посылка данных на сервер
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://myheart.pp.ua/Android_in.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);

//          nameValuePairs.add(new BasicNameValuePair("latitude", stotalLatitude));
//          nameValuePairs.add(new BasicNameValuePair("longitude", stotalLongitude));
            nameValuePairs.add(new BasicNameValuePair("Android_device_time", snowTime));
//          nameValuePairs.add(new BasicNameValuePair("user_info", UserInfo));
//          nameValuePairs.add(new BasicNameValuePair("user_pass", UserPass));
            nameValuePairs.add(new BasicNameValuePair("separator", "______________________________________"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        } catch (NullPointerException e) {
            e.printStackTrace();
        } 

}
}

MainActivity.java

 R.id.menu_send_location_manual: {

             GPSdetermination();
             new PostData().execute();


            // postData(); - вызывает ошибку на реальном устройстве
             Toast.makeText(getApplicationContext(), R.string.location_send_manuall_toast, Toast.LENGTH_LONG).show();
        break;
    }

 public void GPSdetermination() {       
     // create class object
     gps = new GPSTracker(MainActivity.this);

     // check if GPS enabled        
     if(gps.canGetLocation()){

         double latitude = gps.getLatitude();
         double longitude = gps.getLongitude();

         // \n is for new line
         Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();   
     }else{
         // can't get location
         // GPS or Network is not enabled
         // Ask user to enable GPS/network in settings
         gps.showSettingsAlert();
     }

 } 

GPSTracker.java

    public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

//определяем переменную главного активити
    MainActivity ma;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

// Функция для определения местоположения
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 && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                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;
}
/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }       
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}


//события которые происходят если позиция поменялась
@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

When I try to call PostData I get an error java.lang.NullPointerException in this code

double latitudep = gps.getLatitude();
double longitudep = gps.getLongitude();

can not seem to figure out how to fix it.


Solution

  • You have declared GPSTracker gps; inside your AsyncTask, it never seems to get initialised. In other words, it's null. Thus, NullPointerException.

    I guess you need a constructor for your AsyncTask to set it:

    public PostData (GPSTracker gps) {
        this.gps = gps;
    }
    

    Then in MainActivity

    new PostData(gps).execute();
    

    AsyncTask is just a class like any other, with member variables, that need setting, if you want to use them.