Reputation: 5
I have a problem when calling (user.getlat) and (user.getlng). I have declared user object globally, and initialized it in the getUserData() method.
but when I run the app the app crashes and in the logcat is says:
java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.profile.User.getLat()' on a null object reference
public class Profile extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager locationManager;
private LocationListener locationListener;
private FirebaseAuth mAuth;
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("users");
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
mAuth = FirebaseAuth.getInstance();
}
public void getUserData(){
String uID = mAuth.getCurrentUser().getUid();
myRef.child(uID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getChildren();
user = dataSnapshot.getValue(User.class);
Toast.makeText(Profile.this, "" + user.lng, Toast.LENGTH_LONG).show(); // Prints the value normally!
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Profile.this, "Failed to read from database!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng userL = new LatLng(user.getLat(),user.getLng()); // The problem occurs here!
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userL).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userL,17));
}
}
Upvotes: 0
Views: 109
Reputation: 2228
First of all, you haven't called getUserData()
method in anywhere. I read that you tried to add that logic in onCreate()
too, but it didn't work.
Adding a listener doesn't mean that you will get what is expected of it, instantaneously.
Use the onMapReady()
method only to set the map, with settings which is not dependent on User
. Add/Modify the map marker only when you detect changes in the User
data. See the changes:
public void getUserData(){
String uID = mAuth.getCurrentUser().getUid();
myRef.child(uID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getChildren();
user = dataSnapshot.getValue(User.class);
LatLng userL = new LatLng(user.getLat(),user.getLng());
if(mMap!=null){
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userL).title("Your
Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userL,17));
}
Toast.makeText(Profile.this, "" + user.lng, Toast.LENGTH_LONG).show(); // Prints the value normally!
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Profile.this, "Failed to read from database!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
Please call the method getUserData()
or move its code in onCreate()
.
Upvotes: 1