0

I'm getting a rather unhelpful NullPointerException from my junit test of my Azure Functions test service with SpringCloud. I am a complete novice at this, so I apologize if this seems obvious.

The stack trace:

java.lang.NullPointerException   
at org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler.doResolveName(AzureSpringBootRequestHandler.java:86) 
at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.initFunctionConsumerOrSupplierFromCatalog(AbstractSpringFunctionAdapterInitializer.java:387) 
at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.initialize(AbstractSpringFunctionAdapterInitializer.java:124) 
at org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler.handleRequest(AzureSpringBootRequestHandler.java:58) 
at test.TestCloud.start(TestCloud.java:42)

I thought maybe that the NPE was caused by me passing null into the 2nd parameter of handleRequest, but this is what the example I looked at was doing. I've tried with both the commented line or the line below it, using the concrete extension I made or the generic version. Both behaved the same, and control never gets to the execute method. The example uses the generic class, but the specific implementation seemed to make more sense to me (otherwise I don't see how the connection gets made).

    @Test
    public void start() throws Exception {
//        AzureSpringBootRequestHandler<String, String> handler = new AzureSpringBootRequestHandler<String, String>(CloudFunctionApplication.class);
        AzureReverseAdapter handler = new AzureReverseAdapter(CloudFunctionApplication.class);
        String result = handler.handleRequest("Hello World", null);
        handler.close();
        Assert.assertEquals(result, "dlroW olleH");
    }

The test class has just one function in it right now, and it is below.

@FunctionName("reverseString")
public String execute(@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, ExecutionContext context) {

    context.getLogger().info("Input: " + request.getBody().get());
    return handleRequest(request.getBody().get(), context);
}

The CloudFunctionApplication class has a method that look like below. Oddly, and I don't know how, but control DOES get to this function. I thought the execute method was supposed to act as a redirect but it seems to find reserveString anyway. I guess because it is the only @FunctionName?

This is the method. The breakpoint on the return(value) -> { line get hit by the breakpoint, but then the exception happens before the internal gets called.

@Bean
public Function<String, String> reverseString() {
    return (value) -> {
        return new StringBuilder(value).reverse().toString();
    };
}

UPDATE: The linked question absolutely is NOT a duplicate of this question. Yes, all NullPointerExceptions are because something was null when you tried to dereference it. That link was a generic treatment of that. This was a specific question about why I was getting that from a 3rd party library. So they are absolutely NOT the same.

However, I have found the issue as suggested in the comments. It was that I was passing null in as the 2nd parameter. This seemed allowed because of the example I was working on which did the same. My solution was to mock fabricate an ExecutionContext object that returned basic values. Unfortunately, I can't record that as the answer because someone thinks I was just asking what a NullPointerException was and marked the question as a dupe.

So for anyone else who hits the same issue, that was my answer.

Entropy
  • 1,219
  • 6
  • 21
  • 45
  • You are calling `handler.handleRequest()` with 2nd parameter `null` – do you think that's related? – Kaan Sep 23 '19 at 18:09
  • @kaan - I did, but the example I'm working from ALSO does that. – Entropy Sep 23 '19 at 18:38
  • The example I am working from: https://github.com/jdubois/hello-spring-function-azure/blob/master/docs/getting-started-with-spring-cloud-function-in-azure.md – Entropy Sep 23 '19 at 18:39
  • I constructed my own ExecutionContext that returns basic values and it seemed satisfied. – Entropy Sep 23 '19 at 20:59
  • Please provide the test class you are using now. The 'TestCloud.java'. – Cindy Pau Sep 24 '19 at 09:47
  • @Entropy If you solve this problem, you can write an answer and explain the reason for the problem, which will be helpful to others. – Cindy Pau Sep 27 '19 at 08:14

2 Answers2

0

In the example you're basing this on the HelloFunctionTest class has the first statement with no declaration of type between the <> brackets:

AzureSpringBootRequestHandler<User, Greeting> handler = new AzureSpringBootRequestHandler<>(HelloFunction.class);

However, your equivalent (AzureSpringBootRequestHandler as far as I can tell) does, and has (see below). I would start here personally but without a link the the code to have a look around it's hard to tell for certain.

AzureSpringBootRequestHandler<String, String> handler = new AzureSpringBootRequestHandler<String, String>(CloudFunctionApplication.class);

It's quite difficult to follow your question against the given base example as you've gone off on your own tangent before understanding the example. I would recommend following the example through so you really understand it and then move on to your own implementation of it otherwise you'll end up tangled and very confused!

mvee
  • 263
  • 2
  • 15
0

The java.lang.NullPointerException occurs because the object was not instantiated but was used.

Instantiation is to assign the address of the instance to the object, and the NullPointerException is often caused because the instance of the obtained object does not exist, so the returned address is null. Directly referencing this non-existing instance address without judging the error condition will raise this exception.

The output exception of your question tells you the instance had not be create successful. You create instance of AzureSpringBootRequestHandler failed, so then you got a NullPointerException. May something wrong with the Configuration.

This should not be caused by the null value you pass in. The official file was also passed in null. The exception thrown indicates that you have not even successfully created an instance. You try to extend AzureSpringBootRequestHandler and NullPointException comes up, the problem should be that your version of AzureSpringBootRequestHandler does not have the important things that Azure function needed.

These two docs may give you some help:

Spring Cloud Function in Azure

versions of AzureSpringBootRequestHandler.java(Click on the 'branch' in the upper left corner and select the version)

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27