2013-10-15 3 views
0

Я получаю NullPointerException на AlertService#findAll() метода:Java NullPointerException на MongoDB Collection FindAll()

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

Это метод FindAll():

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

Весь AlertService как таковой:

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) {} 
    } 
} 

Возможно, будет легче проверить мой бюстгальтер IntegrateMongo nch на Github, чтобы посмотреть более подробно. https://github.com/georgeotoole/T2ClaimsPortal/tree/IntegrateMongo

Я не могу понять, есть ли проблема с моим кодом или, возможно, монго на моей машине ..?

Благодаря

ответ

1

Я почти уверен, что это случай ...:

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

... не вводили.

Как насчет добавления нулевой проверки в методах, которые используют mongoTemplate, чтобы убедиться, что она была введена?

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); 
} 
+0

это проблема ... теперь просто исправить это! благодаря – Harry