-1

I use Database of Firebase to saved values of latitude and longitude, the problem is that send me this error:

Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

I tried with used a class for methods of onLocationChanged but send the same error.

This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Context Android
    setContentView(R.layout.activity_);
    Firebase.setAndroidContext(this);
    //This  class is to connect with firebase
    Firebase referencia = new Firebase(Path.URL_FIREBASE);

    lat = String.valueOf(location.getLatitude());
    lon = String.valueOf(location.getLongitude());

    LocationClass ll = new LocationClass();
    ll.setLatitud(lat);
    ll.setLongitud(lon);


    Firebase NuevaReferencia = referencia.child("Location").push();
    NuevaReferencia.setValue(ll);




    locationManager = (LocationManager) getSystemService(android.content.Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this.locationListener);




    NuevaReferencia.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {

                LocationClass LL = dataSnapshot.getValue(LocationClass.class);
                String string = "Longitude:"+LL.getLongituded()+"Latitude"+LL.getLatituded();
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });
adjuremods
  • 2,938
  • 2
  • 12
  • 17

1 Answers1

0

From what I can understand, your location object used at lat = String.valueOf(location.getLatitude()); and lon = String.valueOf(location.getLongitude()); seems to be null.

For a better analysis, I ask you to edit your question with the code for this whole class.

Zeh
  • 186
  • 2
  • 8