-1

I want to send a location updates to Firebase when the user Clicks on a button. Updates are set to send every 3 seconds on location change. But the problem is its unable to send the error coming is

Process: com.example.parthtiwari.trace, PID: 7423
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)' on a null object reference

Here is my code

 import android.Manifest;
 import android.content.Context;
import android.content.Intent;

 public class gpdfuel extends AppCompatActivity {

private Button btn;
private Button start;
private FirebaseAuth fb;
private FirebaseAuth.AuthStateListener fbListener;
private DatabaseReference databaseReference;
private String userID;
LocationManager loc;
LocationListener lic;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gpdfuel);
    start = (Button) findViewById(R.id.start);

    fb = FirebaseAuth.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    FirebaseUser user = fb.getCurrentUser();

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            loc.requestLocationUpdates("gps", 5000, 0, lic);
        }
    });

    lic = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            FirebaseUser user = fb.getCurrentUser();
            databaseReference.child(user.getUid()).push().setValue(location.getLatitude() + ",   " + location.getLongitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };

}

KENdi
  • 7,576
  • 2
  • 16
  • 31
Parth Tiwari
  • 466
  • 8
  • 23
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Jul 03 '17 at 18:16

1 Answers1

1

Where you initialize LocationManager loc; ? It is never initialised as per your code that's why you are getting null pointer exception.

Rahul
  • 10,457
  • 4
  • 35
  • 55
  • will this method will give me LatLong everytime it changes.?? – Parth Tiwari Jul 03 '17 at 18:15
  • https://stackoverflow.com/questions/44900352/i-want-to-send-an-real-time-location-updates-to-firebase-when-the-user-clicks-on?noredirect=1#comment76778477_44900352 please solve this – Parth Tiwari Jul 04 '17 at 08:32