2017-05-03 137 views
0

我正在製作一個簡單的jsp頁面,用if .. {return} else語句來處理用戶從表單輸入並獲取跟隨錯誤,請告訴如何解決這個錯誤?JSP錯誤java.lang.NullPointerException

org.apache.jasper.JasperException: An exception occurred processing JSP page 
/create_user.jsp at line 21 

18:   String username=request.getParameter("username"); 
19:   String password=request.getParameter("password"); 
20:   String email=request.getParameter("e-mail"); 
21:   if(username.length()<2 || password.length()<6 || email.length() 
      <5){ 
22:    out.print("Username must be atleast 2 characters long, 
Password must be atleast 6 characters long, E-mail must be atleast 5 
characters long"); 
23:    return; 
24:   }else{ 


Stacktrace: 
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWra 
pper.java:568) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 
70) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

root cause 

java.lang.NullPointerException 
org.apache.jsp.create_005fuser_jsp._jspService(create_005fuser_jsp.java:81) 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 
32) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) – khelwood

+0

嘗試用'email'替換'e-mail'發生的所有事件。 –

+0

謝謝傑瑞,它爲我工作! –

回答

0

我的電子郵件字段懷疑你的形式參數名稱是電子郵件沒有電子郵件。

因此

String email=request.getParameter("e-mail"); --> This will return null 

email.length() will throw Null Pointer Exception 

,我個人遵循的是始終在訪問之前空校驗好的做法。

相關問題