I have an azure function app which is an EventGridTrigger, and I am using gradle. I can build my code and the tests pass. However, when running the task to package my azure functions, the task fails and I get get this exception in terminal:
Execution failed for task ':functionsPackage'.
> java.lang.NullPointerException (no error message)
One of the senior engineers I work with told me he saw the error before, and it was a bug. He said usually it happens with the java 8 streams API lambdas, and I would need to find each lambda and replace with an annoymous class, so I did that. I commented all the azure functions apps code and reintroduced each line of code, piece-by-piece, until I found which one breaks the azureFunctionsPackage task, and I found it to be this method:
private boolean isHandledSubject(SchedulerEventSubject schSubject)
{
// Handle our subscriber ID
//if(SubscriberIds.SWITCH_DMS_ID.equals(schSubject.getSubscriberId()))
//{
// return true;
// }
// If the subscriber ID is ALL and it's a switch or network element
// if(SubscriberIds.ALL_ID.equals(schSubject.getSubscriberId()))
// {
// return ElementType.SWITCH.equals(schSubject.getElementType()) ||
// ElementType.NETWORK.equals(schSubject.getElementType());
// }
//
return false;
}
As you can see, even when I completely commented out all of the method body, it breaks the code and causes the NullPointerException, which I'm confused because I expected it to atleast be one of the methods inside? Even if I make the method void or to return true, it still breaks. This method is called 1 time inside my main EventGridTrigger function called handleEvent (I didn't write any of this code, so I was hoping to avoid any major changes, to avoid breaking something). I will paste the code below, but has anyone seen this before? I'm a bit perplexed. I made changes to this code base on my own branch, and the develop branch compiles/builds fine.
@FunctionName("genericEventHandler")
public void handleEvent(@EventGridTrigger(name = "event") String content) {
try {
if (content == null)
return;
ObjectMapper objMap = new ObjectMapper();
objMap.registerModule(new JodaModule());
EventGridEvent event = SchedulerIcdKt.fromSchedulerEventJson(content);
log.info("EventObject {} ", event);
String schedulerTopic = keyProvider.get(
SCHEDULER_EVENT_GRID_TOPIC_FULL_PATH);
// Check for nulls
if (isBlank(schedulerTopic) || isBlank(event.topic())) {
log.error("Event Grid Topic was NULL! KeyVault:{} Event Topic:{}", schedulerTopic, event.topic());
return;
}
// First check if we even have an event from the Scheduler
if (!event.topic().equalsIgnoreCase(schedulerTopic))
return;
SchedulerEventSubject schSubject = new SchedulerEventSubject(event.subject());
@SuppressWarnings("unchecked")
List<SchedulerEvent> schedulerEventList = (List<SchedulerEvent>) event.data(); //unsafe cast
if (!isHandledSubject(schSubject))
return;
schedulerEventList.stream().forEach(new Consumer<SchedulerEvent>() {
@Override
public void accept(SchedulerEvent schedEvent) {
String networkId;
SnmpCred cred = new SnmpCred();
if (schedEvent.getActionType().equals(SchedulerEventActionType.NetworkCreate)) {
NetworkCreate network_create = (NetworkCreate) schedEvent;
networkId = network_create.getNetworkId();
} else if (schedEvent.getActionType().equals(SchedulerEventActionType.NetworkUpdate)) {
NetworkUpdate network_update = (NetworkUpdate) schedEvent;
networkId = network_update.getNetworkId();
} else {
log.info("Unhandled scheduler data: {}", schedEvent);
return;
}
try {
Optional<Network> network = DataGatewayFactory.getInstance().getNetworkCrud().get(networkId);
if (!network.isPresent()) {
log.error("Error trying to retreive Network with ID {} from the database.", networkId);
return;
}
boolean status;
// If Deployment Type is DEMO, ONLY call deployPtpConfigBlob (no switch config)
if (network.get().getDeploymentType().equals(DeploymentType.DEMO)) {
status = deployPtpConfigBlob(network.get());
} else {
createOrGetNetworkSnmpPassword(network.get(), cred);
if ((status = deploySwitchConfigBlob(network.get(), cred.getAuth(), cred.getPriv()))) {
status = deployPtpConfigBlob(network.get());
}
}
// Return the same scheduler subject with the subject Id set to switch dms
schSubject.setSubscriberId(SubscriberIds.SWITCH_DMS_ID);
EventGridFunctions.this.publishDmsStatus(schSubject, status, schedEvent);
} catch (Exception de) {
log.error("Exception while trying to retreive the network {}.", networkId, de);
}
}
});
} catch (Exception e) {
log.error("EventObject {} ", content, e);
}
}