2012-10-03 4 views
0

자체 인증을 만들려고합니다. 로그인 할 때마다 사용자 이름이 GAE 데이터 저장소에 없으므로 INTERNAL_SERVER_ERROR을 (를) 갖게됩니다.GAE Datastore로 인증하면 엔터티 사용자가 null을 반환합니다.

java.lang.NullPointerException 
at com.pawnsoftware.User.authenticate(User.java:16) 
at com.pawnsoftware.UserLoginServlet.doPost(UserLoginServlet.java:24) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 

등등 ...

방법이 오류를 피하기 않습니다

이 있다고?

오류는 말한다 라인에 있습니다 :

if (user==null) { 
    message = "Username not found."; 
} 

인증하고 사용자 :

public static String authenticate(String username, String password) { 
    String message; 
    Entity user = UserUtil.findUserEntity(username); 
    String pass = user.getProperty("password").toString(); 
    if (user==null) { 
     message = "Username not found."; 
    } else if (user!=null && !password.equals(pass)) { 
     message = "Password is incorrect."; 
    } else if (user!=null && password.equals(pass)) { 
     message = "Successfully logged in!"; 
    } else { 
     message = "Sorry, cannot find the username and password."; 
    } 
    return message; 
} 

사용자 찾기 엔티티 :

: 인증에

public static Entity findUserEntity (String username) { 
    Key userKey = KeyFactory.createKey("User", username); 
    try { 
     return datastore.get(userKey); 
    } catch (EntityNotFoundException e) { 
     return null; 
    } 
} 

업데이트 사용이 null의 경우 16,

public static String authenticate(String username, String password) { String message; Entity user = UserUtil.findUserEntity(username); password = encrypt(password); String pass = ""; try { pass = user.getProperty("password").toString(); } catch (NullPointerException e) { message = "Username not found."; } if (user==null) { message = "Username not found."; } else if (user!=null && !password.equals(pass)) { message = "Password is incorrect."; } else if (user!=null && password.equals(pass)) { message = "Successfully logged in!"; } else { message = "Sorry, cannot find the username and password."; } return message; } 

답변

0

당신은 표시되지 않습니다. 이는 다시, 변수 user에 대한 NullPointerException가 발생할 수 있습니다

user.getProperty("password").toString(); 

여기 가드 문을 추가 할 수 있습니다

if (user != null) { 
    pass = user.getProperty("password").toString(); 
} 
+0

NullPointerException 정보 주셔서 감사. –

0

다음이 줄은 실패 할 것이다 : 당신이 EntityNotFoundException를 얻을 수 및 null을 반환하는 경우

String pass = user.getProperty("password").toString(); 
+1

이 질문에 대한 답변을 제공하지 않습니다. 비평하거나 저자의 설명을 요청하려면 게시물 아래에 의견을 남겨 둡니다. – Jagger

+0

포인트는 @ 재거입니다. –

관련 문제