Question

I get NullPointerException when running mongo template group operation.

The code looks like this:

GroupByResults<Recommendation> recommendations 
    = mongoTemplate.group("recommendation",
          GroupBy.key("doctor"), 
          Recommendation.class
);

The POJO is:

package med.rec.model;

@Document
public class Recommendation extends BaseModel {
    public Recommendation(){

    }


    @Indexed
    @DBRef  
    private Doctor doctor;
    @Indexed
    private String specialty;
    private String x;  
    private int y;
    private int z;
    private int w;
    private String f;
    /*All getter and setters */ 
}

The stack trace seems to be not informative at all but here it is:

ava.lang.NullPointerException
org.springframework.data.mongodb.core.MongoTemplate.group(MongoTemplate.java:1225)
org.springframework.data.mongodb.core.MongoTemplate.group(MongoTemplate.java:1199)
med.rec.controller.api.v1.RecommendationController.all(RecommendationController.java:43)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)

Please advise.

Thank you, Roy

Was it helpful?

Solution

Your usage is not correct, and for a few reasons. First take this "example" from the documentation:

GroupByResults<XObject> results = 
    mongoTemplate.group("group_test_collection", 
        GroupBy.key("x")
        .initialDocument("{ count: 0 }")
        .reduceFunction("function(doc, prev) { prev.count += 1 }"), 
    XObject.class);

And it's result class:

public class XObject {

    private float x;

    private float count;


    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getCount() {
        return count;
    }

    public void setCount(float count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "XObject [x=" + x + " count = " + count + "]";
    }
}

So the first thing you are doing wrong is that you do not have the sufficient arguments specified for the GroupBy builder. This method is sort of a "wrapper" for mapReduce operations, and as such needs a similar style of operations passed to it in order to work.

Secondly, as shown in the example, the class that you pass in as the final argument is for the result and not the "storage" class as you seem to doing. So what would be needed is something that matches the structure of the results that are emitted from the operation, which will not be the same as the "storage" class.

Third point, you seem to be trying to "group by" a field that is a DBRef, and as such you are likely to not get the results you might be expecting. Some reading on that subject might be advised, as the perceived function of a DBRef may not be what you want, and the documentation covers this.

It seems that what you likely want is aggregate, which is also covered in the spring reference documentation after "group" in the first link above. But at any rate you will need to structure the correct arguments for that as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top