私はNULLポインタを取得しています。メソッドの比較方法を理解しておらず、どこが間違っているのか把握しようとしています。アイデアは、私が販売した製品の数で並べ替え、上位5製品を販売することです。私はcompareメソッドを実装すると、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;
}
は私のゲッターは、私はちょうどヌルのチェックを追加する必要がありましたし、すべてが正常に動作しますので、購入を持つようには至っていないいくつかの項目にnullを返していました。
public Integer getCopiesSold() {
if(copiesSold==null){
copiesSold = 0;
}
return copiesSold;
}
エラーログを投稿できますか? –
また、これは何を意味するのですか?> Product.findAll( ""); ? –
あなたの比較にも間違いがあります。else ifは - > else if(p1.getCopiesSold()