0

I'm a newbie to java and Android. Currently I'm doing an IOT project where I had to learn Android Studio by myself to create an app. I'm using thingspeak as the cloud database and everything works fine. I need to plot a map from the Latitude and Longitude data from my GPS sensor and I'm getting the following error

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

Following is my code.

public class Location extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
public static Double Latitude;
public static Double Longitude;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

//getLatitude method
private class getLatitude extends AsyncTask<String, String, String> {
    String content = "";

    @Override
    protected void onPostExecute(String s) {

        Latitude = Double.parseDouble(s);

    }

    @Override
    protected void onProgressUpdate(String... values) {

    }

    @Override
    protected String doInBackground(String... params) {
        content = getContent();
        return content;
    }

    private String getContent() {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        HttpURLConnection urlConnection = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL("https://api.thingspeak.com/channels/channel/fields/1/last?key=APIKey");
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String read;

            while ((read = br.readLine()) != null) {

                sb.append(read);
            }


            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return sb.toString();
    }


}

//getLongitude method
private class getLongitude extends AsyncTask<String, String, String> {
    String content = "";

    @Override
    protected void onPostExecute(String s) {

        Longitude = Double.parseDouble(s);

    }

    @Override
    protected void onProgressUpdate(String... values) {

    }

    @Override
    protected String doInBackground(String... params) {
        content = getContent();
        return content;
    }

    private String getContent() {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        HttpURLConnection urlConnection = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL("https://api.thingspeak.com/channels/channel/fields/2/last?key=APIKey");
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String read;

            while ((read = br.readLine()) != null) {
                sb.append(read);
            }


            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return sb.toString();
    }


}

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    new getLatitude().execute("ddd", "fdf", "dfdf");
    new getLongitude().execute("ddd", "fdf", "dfdf");

    // Add a marker and move the camera
    LatLng location = new LatLng(Latitude,Longitude);
    mMap.addMarker(new MarkerOptions().position(location).title("Device Location Marker"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}

}

  • What are the value you parse as a double from string ? ? – RAAAAM Jun 13 '16 at 16:42
  • I get the Latitude and Longitude values from the cloud. It varies.....The two methods getLatitude and getLongitude takes the values –  Jun 13 '16 at 16:49

0 Answers0