什么是java.lang.NullPointerException

时间:2019-07-16 10:53:09

标签: java authentication nullpointerexception controller

第25行的authentication controller出现问题了
ret = autRequest.body();
原因: java.lang.NullPointerException at com.bfi.bnpparibas.ctrl.PostServiceController.authenticate(PostServiceController.java:25)

是什么原因导致您阻止异常导致程序过早终止?

@PostMapping("/authenticate")
public @ResponseBody LoginResponse authenticate(
  @RequestBody AuthenticationBody ab, HttpServletRequest request, 
  HttpServletResponse response, ModelMap model) {
    String service = "AUTHENTICATION";
    String remoteAddress = request.getRemoteAddr();

    AuthenticationResponse ret;
    String credentialsStr = ab.getUser() + ":" + ab.getPwd();
    String authStr = "Basic " + new String(org.apache.commons.codec.binary.Base64.encodeBase64(credentialsStr.getBytes()));


    LoginResponse loginResponse = new LoginResponse();
    Response<AuthenticationResponse> autRequest = null;
    try {
        autRequest = api.authenticateUserWithCredentials(authStr, ab).execute();
        System.out.println(autRequest);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    try {
        ret = autRequest.body();
        if(ret == null){     
           okhttp3.ResponseBody message = autRequest.errorBody();   
           loginResponse.setMessage(message.string());
           loginResponse.setHasError(true); 
           return loginResponse;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Cookie cookie;
    if (config.isTestCookie()) {
        cookie = new Cookie(config.getCookieName(), config.getTestCookieValue());
    } else {
        cookie = ControllerUtility.extractCookie(request);
    }
    String[] cookieTokens = ControllerUtility.decodeCookie(cookie); 

    String ClasseC2P = cookieTokens[0];
    String LangueC2p = cookieTokens[1];

    Boolean loginAs = false;

    if (cookieTokens.length >= 4) {
         loginAs = Boolean.valueOf(ControllerUtility.splitWithEqual(cookieTokens[3])[1]);
    }
    UserWeb userweb = ret.getUserweb();

    BNPAuthentication authentication = new BNPAuthentication(userweb,
                                                             ret.getServices(),
                                                             ret.getProfiles(),
                                                             ret.getParamBEL(),
                                                             cookie,
                                                             loginAs,
                                                             config.isShowMenu(),
                                                             config.withCredentialsLogin(),
                                                             ClasseC2P,LangueC2p);
    if (authentication.isAuthenticated()) {
        try {
            GlobalDates globalDates = api.globalDates(authentication.getUserWebId(),
                                                      "GETDATES",
                                                      request.getRemoteAddr()).execute().body().get(0);
            authentication.setGlobalDates(globalDates);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }

    loginResponse.setRet(ret);
    loginResponse.setHasError(false);
    loginResponse.setMessage("");
    return loginResponse;
}

2 个答案:

答案 0 :(得分:0)

NullPointerException通常在您尝试对空对象调用方法时发生。

在您的情况下,autRequest可能为空。

尝试调试代码,看看发生了什么。

答案 1 :(得分:0)

您得到一个NullpointerExcpetion,因为您要评估的对象“ autRequest”为空。请尝试调试。

autRequest = api.authenticateUserWithCredentials(authStr, ab).execute();

您的NullpointerExcpetion来自此行