我怎么知道导致NPE的原因?

时间:2020-08-12 23:08:06

标签: java spring spring-boot

我想创建一个传递参数search作为findDuelIdOrEmail方法输入的控制器:

@RequestMapping(value = "/app/friend_search", method = RequestMethod.GET)
public ResponseEntity<List<FriendResponse>> getAll(
        @RequestHeader("Authorization") String authToken,
        @RequestParam(value = "search", required = false) Object search) throws Exception {

    TokenPayload tokenPayload = tokenProvider.verifyAndDecode(authToken);


    List<FriendResponse> friendResponseList = new ArrayList<>();
    boolean isNull = search == null;
    boolean isEmpty = search != null && search.toString().equals("");

    if (isNull || isEmpty) return ResponseEntity.ok(friendResponseList);

    friendResponseList.add(this.friendService.findByDuelIdOrEmail(search));
    return ResponseEntity.ok(friendResponseList);
}

我有一个findDuelIdOrEmail方法,其中包含一个以profile值初始化的变量null。代码如下所示:

public FriendResponse findByDuelIdOrEmail(Object duelIdOrEmail) throws Exception {
    Profile profile = null;
    final String duelIdOrEmailString = duelIdOrEmail.toString();
    final User userEmail = this.userRepository.findByEmail(duelIdOrEmailString);


    if (userEmail == null) {
        final int userId = this.generateUserId(new Long(duelIdOrEmailString));
        profile = this.profileRepository.findByUserId(userId);
    } else {
        profile = this.profileRepository.findByUserId(userEmail.getId());
    }

    if (profile == null) {
        throw new ResourceNotFoundException();
    } else {
        FriendResponse response = this.generateFriendResponse(profile, 1, null);
        checkResponse(response);

        return response;
    }

我的猜测是profile变量引起的NPE。我尝试通过用null验证if(profile == null)值来处理它。但是,我仍然得到NullPointerException

处理此问题的最佳实践是什么?任何提示将不胜感激。

0 个答案:

没有答案
相关问题