Search code examples
javauber-api

Uber Java SDK : rideEstimate, Vehicle and Driver is null


i am building an app and I am using the Uber Java SDK in the Sandbox environment. The problem is the null returned from Uber for RideEstimate, Vehicle and Driver class.

Maven POM:

<dependency>
  <groupId>com.uber.sdk</groupId>
  <artifactId>rides</artifactId>
  <version>0.6.0</version>
</dependency>

My Sample Servlet code:

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (this.getOnceOnly()<2) {


        // Fetch the user's profile.
        UserProfile userProfile = uberRidesService.getUserProfile().execute().body();


        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().printf("Logged in as %s%n", userProfile.getEmail());

        // Publika West Gate 3.171282f 101.664771f
        // Publika East Side 3.170816f 101.667760f
        float startLatitude = 3.170730f;
        float startLongitude =  101.665129f;
        float destinationLatitude = 3.170816f;
        float destinationLongitude = 101.667760f;

        // Get products for location
        Response<ProductsResponse> responseGet = uberRidesService.getProducts(startLatitude, startLongitude).execute();
        List<Product> products = responseGet.body().getProducts();
        String productId = products.get(0).getProductId();
        System.out.println("responseGet: " + responseGet.code() + " " + responseGet.message());

        Response<TimeEstimatesResponse> responseTime = uberRidesService.getPickupTimeEstimate( startLatitude, startLongitude, productId).execute();
        List<TimeEstimate> timeEst = responseTime.body().getTimes();
        System.out.println("responseTime: " + responseTime.code() + " " + responseTime.message());
        //Response<PriceEstimatesResponse> responsePrice = uberRidesService.getPriceEstimates(3.170730f, 101.665129f, 3.171812f, 101.671201f).execute();
        Response<PriceEstimatesResponse> responsePrice = uberRidesService.getPriceEstimates(startLatitude, startLongitude, destinationLatitude , destinationLongitude).execute();
        List<PriceEstimate> priceEst = responsePrice.body().getPrices();
        System.out.println("responsePrice: " + responsePrice.code() + " " +  responsePrice.message());



        // Get upfront fare for product with start/end location
        RideRequestParameters rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
               .setProductId(productId)
               .setDropoffCoordinates(destinationLatitude , destinationLongitude)
               .build();



        Response <RideEstimate> responseRideEstimate = uberRidesService.estimateRide(rideRequestParameters).execute();
        System.out.println("responseRideEstimate: " + responseRideEstimate.code() + " " + responseRideEstimate.message());
        RideEstimate rideEstimate = responseRideEstimate.body();
        //String estFareId = rideEstimate.getEstimate().getFareId();

        System.out.println("\nresponseRideEstimate.errorBody: "+ responseRideEstimate.errorBody());
        System.out.println("\nresponseRideEstimate.rawCode: "+ responseRideEstimate.raw().code());
        System.out.println("\nrideEstimate:"+ rideEstimate +"\n");
        System.out.println("\nrideEstimate.getPickupEstimate: "+ rideEstimate.getPickupEstimate());

        System.out.println("\nrideEstimate.getDisplay: "+ rideEstimate.getFare().getDisplay());
        System.out.println("\nrideEstimate.getFareId: "+ rideEstimate.getFare().getFareId());
        System.out.println("\nrideEstimate.getExpiresAt: "+ rideEstimate.getFare().getExpiresAt());
        System.out.println("\nrideEstimate.getValue: "+ rideEstimate.getFare().getValue());
        System.out.println("\nrideEstimate.getDistanceEstimate: "+ rideEstimate.getTrip().getDistanceEstimate());
        System.out.println("\nrideEstimate.getDistanceUnit: "+ rideEstimate.getTrip().getDistanceUnit());
        System.out.println("\nrideEstimate.getDurationEstimate: "+ rideEstimate.getTrip().getDurationEstimate());

        **// All these will cause an Http Error 500: null returned from Uber**
        //System.out.println("\ngetCurrencyCode: "+ rideEstimate.getEstimate().getCurrencyCode());
        //System.out.println("\ngetFareId: "+ rideEstimate.getEstimate().getFareId());
        //System.out.println("\ngetSurgeConfirmationHref: "+ rideEstimate.getEstimate().getSurgeConfirmationHref());
        //System.out.println("\ngetSurgeConfirmationId: "+ rideEstimate.getEstimate().getSurgeConfirmationId());
        //System.out.println("\ngetMinimum: "+ rideEstimate.getEstimate().getMinimum());
        //System.out.println("\ngetSurgeMultiplier: "+ rideEstimate.getEstimate().getSurgeMultiplier());
        //System.out.println("\ngetHighEstimate: "+ rideEstimate.getEstimate().getHighEstimate());
        //System.out.println("\ngetLowEstimate: "+ rideEstimate.getEstimate().getLowEstimate());
        //System.out.println("\ngetDisplay: "+ rideEstimate.getEstimate().getDisplay());

        String fareId = rideEstimate.getFare().getFareId();

        //double surgeRate = rideEstimate.getEstimate().getSurgeMultiplier();

        System.out.println("options: " +"\n");
        for (int i = 0; i< products.size(); i++ ) { 
            System.out.println(products.get(i).getProductId() +" tt " + products.get(i).getDisplayName() + " Plate " + ""     
                    + " other " + " " + rideEstimate.getFare().getDisplay() 
                    + "" );
            String output = "<br/> Pid "+ products.get(i).getProductId()
                    +" name "+products.get(i).getDisplayName()
                    +" cap "+ products.get(i).getCapacity();
                    //+" other "+ responseTime.message() ;
            response.getWriter().printf(output);
        }

        // Request ride with upfront fare for product with start/end location
        /*RideRequestParameters*/ rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
               .setProductId(productId)
               .setFareId(fareId)
               .setDropoffCoordinates(destinationLatitude , destinationLongitude)
               .build();
        Response <Ride> responseRide = uberRidesService.requestRide(rideRequestParameters).execute();
        System.out.println("responseRide: " + responseRide.code() + " " + responseRide.message());
        //Ride ride = responseRide.body();
        Response<Void> responseUpdateRide = null;

        System.out.println("\nresponseRaw.code: "+ responseRide.raw().code());
        if (responseRide.code()==409) {
            System.out.println("\nresponseRide.errorBody: "+ responseRide.errorBody().string());
            System.out.println("\nresponseRide.errorBody: "+ responseRide.errorBody().contentType().toString());
            System.out.println("\nresponseRide.errorBody: "+ responseRide.headers().names());
            System.out.println("\nuberRidesService.getCurrentRide: "+ uberRidesService.getCurrentRide().toString());
            System.out.println("\nCancelCurrentRide: "+ uberRidesService.getCurrentRide().isCanceled());
            // Due to current user on trip, keep getting Error 409. 
            if (uberRidesService.getCurrentRide().isCanceled()!=true) {
                //uberRidesService.getCurrentRide().cancel();
                System.out.println("Canceling initiated...");
                //Response<Void> responseCancel = uberRidesService.cancelCurrentRide().execute();
                SandboxRideRequestParameters rideAcceptedParameters = new SandboxRideRequestParameters.Builder().setStatus(Ride.Status.DRIVER_CANCELED.toString()).build();
                /*Response<Void>*/ responseUpdateRide = uberRidesService.updateSandboxRide(responseRide.body().getRideId(), rideAcceptedParameters).execute();

                //System.out.println("\nresponseCancel: " + responseCancel.errorBody().toString());
                // Get products again
                System.out.println("Request product from Uber again");
                responseGet = uberRidesService.getProducts(startLatitude, startLongitude).execute();
                List<Product> products2 = responseGet.body().getProducts();
                String productId2 = products2.get(0).getProductId();

                rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
                           .setProductId(productId2)
                           //.setFareId(fareId)
                           .setDropoffCoordinates(destinationLatitude , destinationLongitude)
                           .build();

                responseRideEstimate = uberRidesService.estimateRide(rideRequestParameters).execute();

                rideEstimate = responseRideEstimate.body();

                fareId = rideEstimate.getFare().getFareId();

                rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
                           .setProductId(productId2)
                           .setFareId(fareId)
                           .setDropoffCoordinates(destinationLatitude , destinationLongitude)
                           .build();

                responseRide = uberRidesService.requestRide(rideRequestParameters).execute();
            }
            System.out.println("\nCancelCurrentRide: "+ uberRidesService.getCurrentRide().isCanceled());
        }
        //else {
        Ride ride = responseRide.body();
        System.out.println("\nride: "+ ride);
        System.out.println("\nride.getRideId: "+ ride.getRideId());
        System.out.println("\nride.getStatus(): "+ ride.getStatus());
        System.out.println("\nride.getProductId(): "+ ride.getProductId());
        System.out.println("\nride.getDestination(): "+ ride.getDestination().getLatitude() + ", " +ride.getDestination().getLongitude());
        System.out.println("\nride.getDestination().getEta(): "+ ride.getDestination().getEta()); // this is null
        System.out.println("\nride.getPickup(): "+ ride.getPickup().getLatitude() + ", " +ride.getPickup().getLongitude());
        System.out.println("\nride.getPickup().getEta(): "+ ride.getPickup().getEta()); // this is null
        System.out.println("\nride.getSurgeMultiplier(): "+ ride.getSurgeMultiplier());
        System.out.println("\nride.getVehicle(): "+ ride.getVehicle());
        **// All of the following returned HTTP Error 500: Problem accessing /. Reason: null pointer exception from Uber**
        //System.out.println("\nride.getDriver().getName(): "+ ride.getDriver().getName());
        //System.out.println("\nride.getLocation(): "+ ride.getLocation());
        //System.out.println("\nride.getVehicle(): "+ ride.getVehicle().toString());
        //System.out.println("\nride.getDriver().getName(): "+ ride.getDriver().toString());

        //}

        //System.out.println("\ngetRideId: "+ ride.getRideId());
        //System.out.println("\ngetProductId: "+ ride.getProductId());
        //String rideId = ride.getRideId();

        SandboxRideRequestParameters rideAcceptedParameters = new SandboxRideRequestParameters.Builder().setStatus(Ride.Status.ACCEPTED.toString()).build();
        responseUpdateRide = uberRidesService.updateSandboxRide(responseRide.body().getRideId(), rideAcceptedParameters).execute();

        // Request ride details from request_id
        //responseRide = uberRidesService.getRideDetails(rideId).execute();
        System.out.println("responseRide: " + responseRide.code() + " " + responseRide.message());

        System.out.println("\nride.getStatus(): "+ ride.getStatus());
        System.out.println("\nresponseUpdateRide: " + responseUpdateRide.code() + " " + responseUpdateRide.message());
        if (responseUpdateRide.code()!=202) {
            System.out.println("\nresponseUpdateRide.errorBody: " + responseUpdateRide.errorBody().string());
        }

As you can see from the above, the RideEstimate, Vehicle and Driver classes all lead to null pointers. It is really strange that I can request a ride successfully without a Vehicle or Driver attached to the ride request. I found a workaround to get pass the RideEstimate returned as a null(see my code above) but I cannot find a workaround the Vehicle and Driver being null too.

So, if there is any other way to get the Vehicle and Driver being null, please inform me. Also, it would be nice to have some examples on using the Sandbox update status.


Solution

  • When you request a ride - it gets created with the "processing" state - and at this point, the driver is still unknown - because the driver did not accept this request yet. This is the reason why dont get any driver or vehicle info. The sandbox does not automatically move through a ride but you can manually put ride request through each state:

    "Currently, the sandbox does not change states automatically the way a real request in production would. This endpoint gives the ability to walk an application through the different states of a ride request."

    Please see the sandbox guide for more details: https://developer.uber.com/docs/riders/guides/sandbox.

    To check Lifecycle of a Ride Request please read this documentation.