Reputation: 313
I am trying to retrieve the location for each specific post (Latitude and Longitude) and I am doing this the following way, see code below. I don't understand why I am getting this error:
Can't convert object of type java.lang.String to type com.e.events.Model.Post
I have used this code in other Activities and it worked just fine.
What I need to happen is because each post has its own specific postid
, I want to store that in SharedPreferences and then look through Firebase to find that specific postid
and return that post's specific location Latitude and Longitude. Anyone can explain to me why I am getting this error?
MapsActivityUser.java
public class MapsActivityUser extends FragmentActivity implements OnMapReadyCallback {
String postid;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_user);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PREFS", Context.MODE_PRIVATE);
postid = preferences.getString("postid", "none");
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts").child(postid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (post != null) {
double latitude = post.getLocation().getLatitude();
double longitude = post.getLocation().getLongitude();
LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(location).title("Event location"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
} else {
Toast.makeText(MapsActivityUser.this, "Event doesn't have location on Map", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Post.java
public class Post {
private String postid;
private String postimage;
private String description;
private String publisher;
private String text_event;
private String text_location;
private String text_date_time;
private Long timestamp;
private double Latitude;
private double Longitude;
private LocationHelper locationHelper;
public Post(String description, String postId, String postImage, String publisher, Long timestamp,
String text_event, String text_location, String text_date_time, double latitude, double longitude, LocationHelper location) {
this.postid = postid;
this.postimage = postimage;
this.description = description;
this.publisher = publisher;
this.text_event = text_event;
this.text_location = text_location;
this.text_date_time = text_date_time;
this.timestamp = timestamp;
Latitude = latitude;
Longitude = longitude;
this.locationHelper = location;
}
public Post() {
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPostimage() {
return postimage;
}
public void setPostimage(String postimage) {
this.postimage = postimage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getText_event() {
return text_event;
}
public void setText_event(String text_event) {
this.text_event = text_event;
}
public String getText_location() {
return text_location;
}
public void setText_location(String text_location) {
this.text_location = text_location;
}
public String getText_date_time() {
return text_date_time;
}
public void setText_date_time(String text_date_time) {
this.text_date_time = text_date_time;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public double getLongitude() {
return Longitude;
}
public void setLongitude(double longitude) {
Longitude = longitude;
}
public LocationHelper getLocation() {
return locationHelper;
}
public void setLocation(LocationHelper location) {
this.locationHelper = location;
}
}
LocationHelper.java
public class LocationHelper {
private double Latitude;
private double Longitude;
public LocationHelper(double latitude, double longitude) {
Latitude = latitude;
Longitude = longitude;
}
public LocationHelper() {
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public double getLongitude() {
return Longitude;
}
public void setLongitude(double longitude) {
Longitude = longitude;
}
}
Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'com.e.events.Model.LocationHelper com.e.events.Model.Post.getLocation()' on a null object reference at com.e.events.Map.MapsActivityUser$1.onDataChange(MapsActivityUser.java:53) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.0:55) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Upvotes: 1
Views: 93
Reputation: 6919
After checking your query I can see you passed post_id
so it will give you directly the post data. You don't need to iterate through the DataSnapshot
:
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Post post = dataSnapshot.getValue(Post.class);
if (post != null) {
double latitude = post.getLocation().getLatitude();
double longitude = post.getLocation().getLongitude();
LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(location).title("Event location"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
} else {
Toast.makeText(MapsActivityUser.this, "Event doesn't have location on Map", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Modify Something like this :
You don't need to add Longitude and latitude
public class Post {
private String postid;
private String postimage;
private String description;
private String publisher;
private String text_event;
private String text_location;
private String text_date_time;
private Long timestamp;
private Location locationHelper;
public Post(String description, String postId, String postImage, String publisher, Long timestamp,
String text_event, String text_location, String text_date_time, double latitude, double longitude, LocationHelper location) {
this.postid = postid;
this.postimage = postimage;
this.description = description;
this.publisher = publisher;
this.text_event = text_event;
this.text_location = text_location;
this.text_date_time = text_date_time;
this.timestamp = timestamp;
Latitude = latitude;
Longitude = longitude;
this.locationHelper = location;
}
public Post() {
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPostimage() {
return postimage;
}
public void setPostimage(String postimage) {
this.postimage = postimage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getText_event() {
return text_event;
}
public void setText_event(String text_event) {
this.text_event = text_event;
}
public String getText_location() {
return text_location;
}
public void setText_location(String text_location) {
this.text_location = text_location;
}
public String getText_date_time() {
return text_date_time;
}
public void setText_date_time(String text_date_time) {
this.text_date_time = text_date_time;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Location getLocation() {
return locationHelper;
}
public void setLocation(Location location) {
this.locationHelper = location;
}
}
Upvotes: 2