0

I am working on the Azure Iot Platform, using Java for coding , and facing an issue while sending command to a device. I recieve a null pointer exception at my feedbackReceiver.receiveAsync() method when I dont recieve the feedback for the command I sent. Below is my code snippet.

   public void sendCommandToDevice(String data, String deviceId, boolean gladiusChildFlag) throws Exception {
        LOGGER.info("********* Starting ServiceClient sample...");

        openServiceClient();
        openFeedbackReceiver(deviceId);

        // String commandMessage = data;
        CommandData commandData = new CommandData();
        commandData.setName("ChangeLEDState");
        commandData.setMessageId("123223");
        commandData.setCreatedTime(new Date().toString());
        if(gladiusChildFlag==false)
        {
        HashMap<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("LEDState", data);
        commandData.setParameters(parameter);
        }
        else
        {
        commandData.setGladiusParameters(data); 
        }
        LOGGER.info(commandData.serialize());
        LOGGER.info("********* Sending message to device...");
        Message messageToSend = new Message(commandData.serialize());
        messageToSend.setDeliveryAcknowledgement(DeliveryAcknowledgement.Full);

        // Setting standard properties
        messageToSend.setMessageId(java.util.UUID.randomUUID().toString());
        Date now = new Date();
        messageToSend.setExpiryTimeUtc(new Date(now.getTime() + 60 * 1000));
        messageToSend.setCorrelationId(java.util.UUID.randomUUID().toString());
        messageToSend.setUserId(java.util.UUID.randomUUID().toString());
        messageToSend.clearCustomProperties();

        // Setting user properties
        Map<String, String> propertiesToSend = new HashMap<String, String>();
        propertiesToSend.put("mycustomKey1", "mycustomValue1");
        propertiesToSend.put("mycustomKey2", "mycustomValue2");
        propertiesToSend.put("mycustomKey3", "mycustomValue3");
        propertiesToSend.put("mycustomKey4", "mycustomValue4");
        propertiesToSend.put("mycustomKey5", "mycustomValue5");
        messageToSend.setProperties(propertiesToSend);

        CompletableFuture<Void> completableFuture = serviceClient.sendAsync(deviceId, messageToSend);
        completableFuture.get();

        LOGGER.info("********* Waiting for feedback...");
//recieving null pointer in the line below
        CompletableFuture<FeedbackBatch> future = feedbackReceiver.receiveAsync();
        FeedbackBatch feedbackBatch = future.get();
        LOGGER.info(
                "********* Feedback received, feedback time: " + feedbackBatch.getEnqueuedTimeUtc().toString());

        closeFeedbackReceiver();
        closeServiceClient();

        LOGGER.info("********* Shutting down ServiceClient sample...");
        // System.exit(0);

    }

    protected static void openServiceClient() throws Exception {
        LOGGER.info("Creating ServiceClient...");
        serviceClient = ServiceClient.createFromConnectionString(connectionString, protocol_Service);

        CompletableFuture<Void> future = serviceClient.openAsync();
        future.get();
        LOGGER.info("********* Successfully created an ServiceClient.");
    }

    protected static void closeServiceClient() throws ExecutionException, InterruptedException, IOException {
        serviceClient.close();

        CompletableFuture<Void> future = serviceClient.closeAsync();
        future.get();
        serviceClient = null;
        System.out.println("********* Successfully closed ServiceClient.");
    }

    protected static void openFeedbackReceiver(String deviceId) throws ExecutionException, InterruptedException {
        if (serviceClient != null) {
            feedbackReceiver = serviceClient.getFeedbackReceiver(deviceId);
            if (feedbackReceiver != null) {
                CompletableFuture<Void> future = feedbackReceiver.openAsync();
                future.get();
                LOGGER.info("********* Successfully opened FeedbackReceiver...");
            }
        }
    }

    protected static void closeFeedbackReceiver() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = feedbackReceiver.closeAsync();
        future.get();
        feedbackReceiver = null;
        LOGGER.info("********* Successfully closed FeedbackReceiver.");
    }

Thanks in advance for some help in this issue :)

  • It seems that you have refered to the sample code https://github.com/Azure/azure-iot-sdks/blob/master/java/service/samples/service-client-sample/src/main/java/samples/com/microsoft/azure/iot/service/sdk/ServiceClientSample.java. Did you change any code? Could you supply the exception information in detail? – Peter Pan Jun 03 '16 at 09:22
  • Any update for more details? – Peter Pan Jun 10 '16 at 09:05
  • Hey Peter.. Sorry for delayed response. I checked again but the problem seems to persist now . I have not made any change to the sample Code which I used initially. What all could be the reason for this ? – Kamal Chaturvedi Jun 12 '16 at 14:03

0 Answers0