Fix issue causing intent without request handler to throw a NullPointerException (NPE), was not fixed in previous PR: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/312. isPresent is not the same as check is the Optional object itself is null. isPresent checks if the value of the Optional is null.
Description
An intent when added to the interaction model may not have a corresponding RequestHandler. While its acceptable that the response is null or empty as a result, without the null check update in the PR, a NullPointerException is thrown by the skill. This is because the Optional have "value" field and not having the RequestHandler causes the Optional object itself to be null. When the value field of a null object is accessed a NPE is thrown.
Motivation and Context
Avoid throwing an NPE in case of missing RequestHandler, instead handle the scenario more gracefully by providing an empty or null response.
Testing
Created a skill with an interaction model with an intent without a corresponding RequestHandler. After the update in this PR, the response from the skill was null whereas before this PR the skill threw a NPE.
Skill behavior before the change:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.Optional.orElse(java.lang.Object)' on a null object reference
at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:149)
at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:38)
at com.amazon.ask.impl.AbstractSkill.execute(AbstractSkill.java:84)
at com.amazon.ask.SkillStreamHandler.handleRequest(SkillStreamHandler.java:84)
Fix issue causing intent without request handler to throw a NullPointerException (NPE), was not fixed in previous PR: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/312. isPresent is not the same as check is the Optional object itself is null. isPresent checks if the value of the Optional is null.
Description
An intent when added to the interaction model may not have a corresponding RequestHandler. While its acceptable that the response is null or empty as a result, without the null check update in the PR, a NullPointerException is thrown by the skill. This is because the Optional have "value" field and not having the RequestHandler causes the Optional object itself to be null. When the value field of a null object is accessed a NPE is thrown.
Motivation and Context
Avoid throwing an NPE in case of missing RequestHandler, instead handle the scenario more gracefully by providing an empty or null response.
Testing
Created a skill with an interaction model with an intent without a corresponding RequestHandler. After the update in this PR, the response from the skill was null whereas before this PR the skill threw a NPE.
Skill behavior before the change:
Response JSON from skill after the change:
Types of changes
Checklist
License