0

I am getting a NullPointerException on AlertService#findAll() method:

java.lang.NullPointerException
com.t2.claims.services.AlertService.findAll(AlertService.java:24)
com.t2.claims.controllers.AlertIndexController.doAfterCompose(AlertIndexController.java:28)

This is the findAll() method:

public List<Alert> findAll() {
    Query query = new Query(where("id").exists(true));
    return mongoTemplate.find(query, Alert.class);
}

The whole AlertService is as such:

package com.t2.claims.services;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.t2.claims.models.Alert;
import static org.springframework.data.mongodb.core.query.Criteria.where;

@Service("alertService")
@Transactional
public class AlertService {

    @Resource(name="mongoTemplate")
    private MongoTemplate mongoTemplate;

    public List<Alert> findAll() {
        Query query = new Query(where("id").exists(true));
        return mongoTemplate.find(query, Alert.class);
    }

    public void add(Alert alert) {
        try {
            mongoTemplate.insert(alert);
        } catch(Exception e) {}
    }
    public void update(Alert alert) {
        Query query = new Query(where("id").is(alert.getId()));
        try {
            Update update = new Update();
            update.set("assignedUser", alert.getAssignedUser());
            update.set("status", alert.getStatus());
            update.set("category", alert.getCategory());
            update.set("vehicleStatus", alert.getVehicleStatus());
            update.set("brand", alert.getBrand());
            mongoTemplate.updateMulti(query, update, Alert.class);
        } catch(Exception e) {}
    }
    public void delete(Alert alert) {
        try {
            Query query = new Query(where("id").is(alert.getId()));
            // Run the query and delete the entry
            mongoTemplate.remove(query, Alert.class);
        } catch(Exception e) {}
    }
}

It may be easier to check out my IntegrateMongo branch on Github to have at look in more detail. https://github.com/georgeotoole/T2ClaimsPortal/tree/IntegrateMongo

I can't understand if there is an issue with my code or perhaps mongo on my machine .. ?

Thanks

1 Answer 1

1

I'm pretty certain it's a case of... :

@Resource(name="mongoTemplate")
private MongoTemplate mongoTemplate;

...not being injected.

What about adding a null check in the methods that use mongoTemplate to make sure that it has been injected?

public List<Alert> findAll() {
    Query query = new Query(where("id").exists(true));
    if (mongoTemplate == null) {
        throw new IllegalStateException("mongoTemplate is null");
    }
    return mongoTemplate.find(query, Alert.class);
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is the issue... now just to fix it! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.