'Spring cloud function in azure throws null pointer exception
I have spring cloud function deployed to azure function app.The API works perfectly fine in the local developer machine. Whereas when deployed to azure, upon calling from postman/client throws a null pointer exception. Java Run time Version : 11 Function Extension Version : ~3
2022-02-25T10:14:10.644 [Information] 2022-02-25 10:14:10.629 ERROR 2880 --- [pool-2-thread-1] : Error null
2022-02-25T10:14:10.645 [Information] java.lang.NullPointerException
2022-02-25T10:14:10.645 [Information] at org.springframework.cloud.function.adapter.azure.FunctionInvoker.handleRequest(FunctionInvoker.java:129)
2022-02-25T10:14:10.645 [Information] at ProductController.getProduct(ProductController.java:53)
Below is the snippet from the product controller where the exception is thrown.
@FunctionName("getProduct")
public HttpResponseMessage getProduct(@HttpTrigger(name = "getRequest", methods = {
HttpMethod.GET }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<String> getProductRequest,
ExecutionContext context) {
ProductRequestDTO productRequestDTO = new ProductRequestDTO();
try {
String id = getProductRequest.getQueryParameters().get("id");
if (id != null) {
log.info("Request Received for id: " + id);
productRequestDTO.setId(id);
}
return getProductRequest.createResponseBuilder(HttpStatus.OK).body(handleRequest(productRequestDTO,context)).build();
}
catch (NullPointerException e) {
l
log.error("Error " + e.getMessage());
return getProductRequest.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
pom.xml
=============
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<azure.functions.maven.plugin.version>1.14.2</azure.functions.maven.plugin.version>
<azure.functions.java.library.version>1.4.2</azure.functions.java.library.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azur
e</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-webflux</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
Solution 1:[1]
- Because the object was not instantiated but used, the
java.lang.NullPointerException
occurs. - The purpose of instantiation is to assign the instance's address to the object, and the
NullPointerException
is frequently thrown because the obtained object's instance does not exist, therefore the returned address is null. This exception will be thrown if you refer to this non-existing instance address without first determining the problem situation. - You received a NullPointerException because your attempt to create an instance of AzureSpringBootRequestHandler failed. There could be an issue with the configuration.
- The null value you entered should not be the cause of this. The official file was also also passed in null. The thrown exception implies that you haven't completed creating an instance sucessfully.
- If you try to extend AzureSpringBootRequestHandler and get a NullPointException, the issue is likely that your version of AzureSpringBootRequestHandler lacks the important features that Azure requires.
Please refer these links for more information.
Solution 2:[2]
We had a similar issue and solved it by updating Function Extension Version: ~4 and a few other dependency versions as listed below
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-dependencies</artifactId>
<version>3.2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library</artifactId>
<version>${azure.functions.java.library.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>4.1.5</version>
</plugin>
</plugins>
</pluginManagement>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SuryasriKamini-MT |
Solution 2 | PrabhuPerumal |