2013-03-22 1 views
-1

나는 JSP와 함께이 양식 필드를 처리 할 :Null 포인터 예외가 발생하는 폼 처리 중입니까?

@Override 
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     logger.log(Level.INFO, "Creating User!!!"); 
     logger.info("Request: " + req.toString()); 
     PrintWriter out = resp.getWriter();//Here I get the null Pointer exception 

     String email = req.getParameter("email"); 
     String password = req.getParameter("password"); 
     logger.info(email + password); 
     try { 
      user.insert(email, password); 
      Log.info("Inserted: " + email + " " + password); 
     } catch (Exception e) { 
      String msg = DAOUser.getErrorMessage(e); 
      out.print(msg); 
     } 
    } 

:

<div class="control-group"> 

               <!-- Text input--> 
               <label class="control-label" for="input01">Email:</label> 
               <div class="controls"> 
                <input name="email" placeholder="email" 
                 class="input-xlarge" type="text" 
                 value="<%=request.getParameter("email")%>"> 
               </div> 
              </div> 
              <div class="control-group"> 
               <!-- Text input--> 
               <label class="control-label" for="input01">Password:</label> 
               <div class="controls"> 
                <input name="password" placeholder="password" 
                 class="input-xlarge" type="text" 
                 value="<%=request.getParameter("password")%>"> 
               </div> 
              </div> 

내가이

NullPointerException

내 서블릿 방법을 먹으 렴 얻을 제출을 누르면.

이 널 포인터 예외를 수정하는 방법은 무엇입니까?

UPDATE

내 서블릿 :

public class DAOServletUser extends HttpServlet { 

    private static final long serialVersionUID = 6820994892755862282L; 

    private static final Logger logger = Logger.getLogger(DAOServletUser.class.getCanonicalName()); 
    /** 
    * Get the entities in JSON format. 
    */ 

    public DAOServletUser() { 
     super(); 
    } 

    public IDAOUser user; 

    /** 
    * Create the entity and persist it. 
    */ 
    @Override 
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     logger.log(Level.INFO, "Creating User!!!"); 
     logger.info("Request: " + req.toString()); 

     if(resp==null) { 
      System.out.println("Respond is NULL!!!"); 
     } 

     PrintWriter out = resp.getWriter();//Here I get the null Pointer exception 



     String email = req.getParameter("email"); 

     if(email==null) { 
      System.out.println("email is null"); 
     } else { 
      System.out.println("email is NOT null"); 
     } 
     String password = req.getParameter("password"); 
     if(password==null) { 
      System.out.println("password is null"); 
     } else { 
      System.out.println("password is NOT null"); 
     } 
     logger.info(email + "::" + password); 
     try { 
      user.insert(email, password); 
      Log.info("Inserted: " + email + " " + password); 
     } catch (Exception e) { 
      String msg = DAOUser.getErrorMessage(e); 
      out.print(msg); 
     } 
    } 
+0

정확한 오류 메시지 (stracktrace)를 줄 수 있습니까? 그 문제가 무엇인지 정확히 알기가 어렵습니다. –

+0

어떤 라인이 에러를 던지고 있는지 알려주십시오 - 예외의 스택 트레이스는 정보를 갖습니다 – Bohemian

+0

죄송합니다. 스택 트레이스가 없습니다. 'Error : java.lang.NullPointerException' 페이지 만 가져옵니다. – maximus

답변

2

내가 IDAOUser는 인터페이스입니다 있으리라 믿고있어. 자바에서 인터페이스를 구현하려면, 당신과 같이 클래스 수준에서 implements 키워드를 사용

public class DaoUserImpl implements IDAOUser { 
    public void insert(String email, String password) { 
     // your code for inserting goes here 
    } 
} 

서블릿 클래스에서 대신이 같은

public IDAOUser user; 

인스턴스화합니다 (new 키워드를 사용)

public IDAOUser user = new DaoUserImpl(); 

그런 다음 null 아닌 객체에 user.insert(email,password)를 호출 할 수 있습니다.

DaoUserImpl이 공유 인스턴스 데이터를 사용하는 경우이 솔루션에서 발생할 수있는 멀티 스레딩 문제에주의해야합니다. 요청 당 하나의 클래스 인스턴스가 필요할 수 있습니다.

관련 문제