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);
}
};
}