null 포인터가 발생했습니다. 비교 방법에 익숙하지 않고 잘못된 위치를 파악하려고합니다. 아이디어는 내가 판매하는 제품의 수를 기준으로 정렬 한 다음 판매되는 상위 5 개 제품을 얻는 것입니다. 그것은 일단 비교 메서드를 구현하면 NullPointer를 반환합니다.개체 목록 별 정렬 정렬
public Result index() {
// Get list of all categories in ascending order
String name = "Best Sellers";
List<Category> categoriesList = Category.findAll();
List<Product> productsList;
Long cat = new Long("11");
productsList = bestSellers();
return ok(index.render(env, categoriesList, productsList, cat, "", getCurrentUser(), name));
}
public List<Product> bestSellers(){
List<Product> temp = Product.findAll("");
Collections.sort(temp, new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
if(p1.getCopiesSold()>p2.getCopiesSold()){
return 1;
} else if(p1.getCopiesSold()<p2.getCopiesSold()){
return -1;
}
return 0;
}
});
List<Product> bestSellers = new ArrayList<>();
for(int i=0; i<5; i++){
bestSellers.add(temp.get(i));
}
return bestSellers;
}
getter가 아직 구입하지 않은 항목에 대해 null을 반환 했으므로 null에 대한 검사를 추가하면 모든 것이 잘됩니다.
public Integer getCopiesSold() {
if(copiesSold==null){
copiesSold = 0;
}
return copiesSold;
}
오류 로그를 게시 할 수 있습니까? –
도 마찬가지입니다. -> Product.findAll (""); ? –
당신의 비교에서 실수가 있습니다. else if는 -> else if (p1.getCopiesSold()