我正在研究数据表的排序功能。我有 JSF Web 控制器:
@Named
@ViewScoped
public class SearchPlayerController implements Serializable {
private List<Player> playerList;
@EJB
PlayerFacade playerFacade;
@PostConstruct
public void init() {
if (playerList == null) {
playerList = playerFacade.findAll();
}
}
// getters and setters
*
*
*
}
在这个控制器中,我有一个排序方法:
public String sortDataByClubName(final String dir) {
Collections.sort(playerList, (Player a, Player b) -> {
if(a.getClubId().getClubName()
.equals(b.getClubId().getClubName())) {
return 0;
} else if(a.getClubId().getClubName() == null) {
return -1;
} else if(b.getClubId().getClubName() == null) {
return 1;
} else {
if(dir.equals("asc")) {
return a.getClubId().getClubName()
.compareTo(b.getClubId().getClubName());
} else {
return b.getClubId().getClubName()
.compareTo(a.getClubId().getClubName());
}
}
});
return null;
}
在页面视图上调用排序后,它会抛出NullPointerException
. 我认为主要原因是它内部Comparator
无法读取clubName
获得 Club 对象后应该可以访问的值。有没有可能比较嵌套属性的值?