Fairly new to Android development, following a tutorial on Udemy. Getting a nullpointerexception on an object, even though it prints the value to the log. Looks like it's the driverLocation object that's messing up, but I have tried to initialize it in the declaration and in onCreate. I am using parse.com to save data.
Below is the code from the java file, or activity this error occurs. There is 4 other java files in the project, so tell me if I should post more. Posted log messages below code. Been stuck on this for a while, and there is most certainly something obvious iv'e forgotten.
public class YourLocation extends FragmentActivity implements OnMapReadyCallback, LocationListener {
Location location;
private GoogleMap mMap;
LocationManager locationManager;
String provider;
TextView infoTextView;
Button requestDriverButton;
Boolean requestActive;
String driverUsername = "";
ParseGeoPoint driverLocation = new ParseGeoPoint(10,10);
Handler handler = new Handler();
public void requestDriver(View view) {
if (requestActive == false) {
Toast.makeText(getApplicationContext(), "Driver Requested", Toast.LENGTH_SHORT).show();
final ParseObject request = new ParseObject("Request");
request.put("requesterUsername", ParseUser.getCurrentUser().getUsername());
ParseACL parseACL = new ParseACL();
parseACL.setPublicWriteAccess(true);
parseACL.setPublicReadAccess(true);
request.setACL(parseACL);
request.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
infoTextView.setText("Waiting for driver...");
requestDriverButton.setText("Cancel ride");
requestActive = true;
updateLocation(location);
}
}
});
} else {
infoTextView.setText("Ride cancelled");
requestDriverButton.setText("Request Driver");
requestActive = false;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Request");
query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseObject object : objects) {
object.deleteInBackground(new DeleteCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(getApplicationContext(), "Ride Cancelled", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_location);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
infoTextView = (TextView) findViewById(R.id.infoTextView);
requestDriverButton = (Button) findViewById(R.id.requestDriver);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); //Context.LOCATION_SERVICE??
provider = locationManager.getBestProvider(new Criteria(), false);
locationManager.requestLocationUpdates(provider, 400, 1, this);
requestActive = false;
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
updateLocation(location);
}
}
public void updateLocation(final Location location) {
mMap.clear();
if (requestActive == false) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Request");
query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseObject object : objects) {
requestActive = true;
infoTextView.setText("Waiting for driver...");
requestDriverButton.setText("Cancel ride");
if (object.get("driverUsername") != null) {
driverUsername = object.getString("driverUsername");
infoTextView.setText("A driver is on the way");
requestDriverButton.setVisibility(View.INVISIBLE);
}
}
}
}
}
});
}
if (driverUsername.equals("")) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location"));
}
if (requestActive == true) {
if (!driverUsername.equals("")) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", driverUsername);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseUser driver : objects) {
driverLocation = driver.getParseGeoPoint("location");
}
}
}
}
});
Log.i("test", driverLocation.toString());
Log.i("latitude", String.valueOf(driverLocation.getLatitude()));
Log.i("longitude", String.valueOf(driverLocation.getLongitude()));
if (driverLocation.getLatitude() != 0.0 && driverLocation.getLongitude() != 0.0) {
Log.i("app", driverLocation.toString());
Double distanceInKM = driverLocation.distanceInKilometersTo(new ParseGeoPoint(location.getLatitude(), location.getLongitude()));
NumberFormat format = new DecimalFormat("#0.00");
infoTextView.setText("Your driver is " + format.format(distanceInKM) + " km away");
LatLngBounds.Builder builder = new LatLngBounds.Builder();
ArrayList<Marker> markers = new ArrayList<>();
markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(driverLocation.getLatitude(), driverLocation.getLongitude())).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)).title("Driver Location")));
markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location")));
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 300;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cameraUpdate);
}
}
final ParseGeoPoint userLocation = new ParseGeoPoint(location.getLatitude(), location.getLongitude());
ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("Request");
query1.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query1.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (ParseObject object : objects) {
object.put("requesterLocation", userLocation);
object.saveInBackground();
}
}
}
});
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
updateLocation(location);
}
}, 2000);
}
@Override
public void onLocationChanged(Location location) {
mMap.clear();
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Logcat:
11-01 23:30:43.447 11709-11709/com.parse.starter I/test: ParseGeoPoint[10.000000,10.000000]
11-01 23:30:43.447 11709-11709/com.parse.starter I/latitude: 10.0
11-01 23:30:43.447 11709-11709/com.parse.starter I/longitude: 10.0
11-01 23:30:43.447 11709-11709/com.parse.starter I/app: ParseGeoPoint[10.000000,10.000000]
11-01 23:30:43.967 11709-11709/com.parse.starter D/AndroidRuntime: Shutting down VM
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: FATAL EXCEPTION: main
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: Process: com.parse.starter, PID: 11709
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseGeoPoint.toString()' on a null object reference
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at com.parse.starter.YourLocation.updateLocation(YourLocation.java:218)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at com.parse.starter.YourLocation$6.run(YourLocation.java:275)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-01 23:30:43.968 11709-11709/com.parse.starter E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)