-2

I have a problem in the authentication controller in line 25
ret = autRequest.body();
Caused by: java.lang.NullPointerException at com.bfi.bnpparibas.ctrl.PostServiceController.authenticate(PostServiceController.java:25)

What is the cause so that you stop the exception from causing the program to terminate prematurely?

@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;
}
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17

2 Answers2

0

NullPointerException occurs most of the time when you try to call a method on a null object.

In your case, autRequest may be null.

Try to debug your code and see what is going on.

Sylvain D
  • 1
  • 2
0

You getting a NullpointerExcpetion because the object "autRequest" that you are trying to assess is null. please try debugging.

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

your NullpointerExcpetion is coming from this line

mrobi
  • 17
  • 10