2017-02-21 58 views
0

我正在使用wildfly 10.1.0我的項目運行良好,但是當我嘗試通過從數據庫中檢索數據來驗證用戶名和密碼時,我得到空指針異常。連接到數據庫但無法獲取數據結果方法拋出空指針異常

Caused by: java.lang.NullPointerException 
 
\t at com.revguru.crs.web.user.controller.UserLoginController.authenticate(UserLoginController.java:86) 
 
\t at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 
\t at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
 
\t at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
 
\t at java.lang.reflect.Method.invoke(Method.java:498) 
 
\t at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181) 
 
\t at com.sun.el.parser.AstValue.invoke(AstValue.java:289) 
 
\t at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) 
 
\t at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 
 
\t at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 
 
\t at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 
 
\t at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 
 
\t at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
 
\t at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
 
\t ... 50 more 
 

當我平安我的數據庫顯示我已連接,但仍然有securityService.authenticate(用戶名,密碼)方法調用我的UerLoginController類拋出空指針異常

UerLoginController類

import java.util.ArrayList; 
 
import java.util.Date; 
 
import java.util.Enumeration; 
 
import java.util.HashMap; 
 
import java.util.List; 
 
import java.util.Locale; 
 
import java.util.ResourceBundle; 
 
import java.util.TimeZone; 
 

 
import javax.faces.context.FacesContext; 
 
import javax.servlet.http.HttpSession; 
 

 
import com.revguru.crs.core.entity.ht.HotelDataObject; 
 
import com.revguru.crs.core.service.exception.HSDServiceException; 
 
import com.revguru.crs.core.service.sc.SecurityService; 
 
import com.revguru.crs.core.util.HSDServiceConstants; 
 
import com.revguru.crs.security.entity.FeatureDataObject; 
 
import com.revguru.crs.security.entity.HotelUserDataObject; 
 
import com.revguru.crs.security.entity.RoleDataObject; 
 
import com.revguru.crs.security.entity.UserDataObject; 
 
import com.revguru.crs.web.common.vo.CommonGlobalBean; 
 
import com.revguru.crs.web.constants.HSDWebConstants; 
 
import com.revguru.crs.web.hotel.controller.GeneralHotelController; 
 
import com.revguru.crs.web.hotel.delegate.HotelDelegate; 
 
import com.revguru.crs.web.user.delegate.UserDelegate; 
 
import com.revguru.crs.web.user.vo.Menu; 
 
import com.revguru.crs.web.user.vo.MenuItem; 
 
import com.revguru.crs.web.user.vo.UserInformation; 
 
import com.revguru.crs.web.user.vo.UserLogin; 
 
import com.revguru.crs.web.utils.Util; 
 

 
public class UserLoginController { 
 
\t 
 
\t private FacesContext facesContext = FacesContext.getCurrentInstance(); 
 
\t HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false); 
 
\t private SecurityService securityService = new SecurityService() ; 
 
\t 
 
\t public void setSecurityService(SecurityService securityService) { 
 
\t \t this.securityService = securityService; 
 
\t } 
 
\t 
 
\t public SecurityService getSecurityService() { 
 
\t \t return this.securityService; 
 
\t } 
 
\t 
 
\t /** 
 
\t * Authenticate the User 
 
\t * @return 
 
\t */ 
 
\t public String authenticate() { 
 
\t \t 
 
\t \t if(session.getAttribute("menus") != null) { 
 
\t \t \t session.removeAttribute("menus"); 
 
\t \t } 
 
\t \t 
 
\t \t if (session.getAttribute("CommonGlobalBean") != null) { 
 
\t \t \t session.removeAttribute("CommonGlobalBean"); 
 
\t \t } 
 
\t \t 
 
\t \t CommonGlobalBean commonGlobalBean = new CommonGlobalBean(); 
 
\t \t commonGlobalBean.setTimeZone(TimeZone.getDefault().getID()); 
 

 
\t \t UserLogin userLogin = new UserLogin(); 
 
\t \t userLogin = (UserLogin)session.getAttribute("UserLogin"); 
 
\t \t 
 
\t \t String username = userLogin.getUserName(); 
 
\t \t String password = userLogin.getPassword(); 
 
\t \t 
 
\t \t UserInformation userInformation = null; \t 
 
\t \t 
 
\t \t if (userLogin != null) { 
 
\t \t \t if (securityService.authenticate(username,password)) { 
 
\t \t \t \t Util.log("Get User Object by Email"); 
 
\t \t \t \t UserDataObject userDataObject = securityService.getUserObjectByEmail(userLogin.getUserName()); 
 
\t \t \t \t Util.log("User name : "+userDataObject.getUserFirstName()); 
 
\t \t \t \t userInformation = (UserInformation)session.getAttribute("UserObject"); 
 
\t \t \t \t if (userInformation != null) { 
 
\t \t \t \t \t session.removeAttribute("UserObject"); 
 
\t \t \t \t } 
 
\t \t \t \t userInformation = getUserInformation(userDataObject); 
 
\t \t \t \t session.setAttribute("UserObject", userInformation); 
 
\t \t \t \t \t \t 
 
\t \t \t \t if(!checkUserAsHotelRole(userInformation)) { 
 
\t \t \t \t \t Util.log("User does not have hotel role."); 
 
\t \t \t \t \t Util.reportError(facesContext, "login_user_not_valid_password", null); 
 
\t \t \t \t }
這是我的方法定義

public boolean authenticate(String email, String password) { 
 
\t \t log.info("Authenticating : " + email); 
 
\t \t UserDataObject user = new UserDataObject(); 
 
\t \t \t user=getUserObjectByEmail(email); 
 
\t \t if (user == null) { 
 
\t \t \t log.info("User does not exist for email id :" + email); 
 
\t \t \t return false; 
 
\t \t } 
 
\t \t return checkPassword(password, user.getPassword()); 
 
\t }

+0

你得到的錯誤是什麼? – mlg

+0

在這一行如果(securityService.authenticate(username,password)){ –

回答

0

以下行是有問題的:

UserLogin userLogin = new UserLogin(); 
userLogin = (UserLogin)session.getAttribute("UserLogin"); 

在第一行中,可以分配一個參照本發明的新的用戶登陸對象變量userLogin

在第二行中,您將該引用替換爲會話中鍵「UserLogin」下存在的對象的引用。

如果會話中不包含具有「UserLogin」鍵的屬性,則結果爲null,導致在其正下方的行中出現NullpointerException:String username = userLogin.getUserName();

至於在那裏你指出的問題的地方本來是:假設既不username也不passwordnull,這個問題將在裏面authenticate(String, String),而你沒有的代碼提供給我們。

+0

請你詳細說明你的答案我沒有得到它 –