아제발요살려주세요
30
2019-09-12 18:43:37
4
249

아이디 비밀번호 찾기하는데 자꾸 java.lang.NullPointerException 오류가 떠요,,


아이디비밀번호찾기 

find.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>아이디비밀번호찾기</title>
</head>
<body>
<p>아이디찾기</p>
   <form action="find_pro_id.jsp" method="post">
    이름 <input type="text" name="userirum" placeholder="이름" required><br>
    전화번호 <input type="text" name="usertel" placeholder="전화번호 - 제외" required><br>
    이메일 <input type="text" name="useremail" placeholder="이메일" required><br>
    <input type="submit" value="아디찾기"><br>
</form>
           <p>비밀번호찾기</p>
            <form action="find_pro_pwd.jsp" method="post">
                이름<input type="text" name="userirum" placeholder="이름" required><br>
                아이디<input type="text" name="userid" placeholder="아이디" required><br>
                이메일<input type="text" name="useremail" placeholder=" 이메일" required><br>
        <input type="submit" value="비번찾기">
        </form>
</body>
</html>

find_pro_id.jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@page import="com.user.MemberDAO" %>
    <%@page import="com.user.MemberUser" %>
    
<!DOCTYPE html>
<html>
<head>

<jsp:useBean id="muser" class ="com.user.MemberUser"/>
<jsp:setProperty property="*" name="muser"/>  
<%

	request.setCharacterEncoding("euc-kr");

MemberDAO dao = MemberDAO.getInstance();
	
String userid;
userid = dao.Find_id(muser);

String userirum = request.getParameter("userirum");
String usertel = request.getParameter("usertel");
String useremail = request.getParameter("useremail");

if(userid == null){

%>

<title>아이디찾기</title>
</head>
<body>
<script>
alert("입력한정보가 올바르지않거나 없음")
document.location.href="find.jsp";
</script>
<%
}else{
%>
<script>
alert("아이디 <%=userid%>");
document.location.href="login.jsp";

</script>
<%} %>
</body>
</html>

find_pro_pwd.jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@page import="com.user.MemberDAO" %>
    <%@page import="com.user.MemberUser" %>
    
<!DOCTYPE html>
<html>
<head>

<jsp:useBean id="muser" class ="com.user.MemberUser"/>
<jsp:setProperty property="*" name="muser"/>  
<%
	request.setCharacterEncoding("euc-kr");

MemberDAO dao = MemberDAO.getInstance();
	
String pwd=null;

pwd = dao.Find_pwd(muser);

if(pwd ==null){

%>

<title>아이디찾기</title>
</head>
<body>
<script>
alert("입력한정보가 올바르지않거나 없음")
document.location.href="find.jsp";
</script>
<%
}else{
%>
<script>
alert("비번 <%=pwd%>");
document.location.href="login.jsp";

</script>
<%} %>
</body>
</html>

MemberDAO.java


package com.user;

import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;

public class MemberDAO {

	public static final int MEM_NONEXIST = 0;
	public static final int MEM_EXIST = 1;
	public static final int MEM_JOIN_SUCCESS = 1;
	public static final int MEM_JOIN_FAIL = 0;
	public static final int MEM_LOGIN_SUCCESS = 1;
	public static final int MEM_LOGIN_PW_NOT = 0;
	public static final int MEM_LOGIN_NOT = -1;

	
	private static MemberDAO dao;
	
	private DataSource ds;
	private Connection con;
	private Context cnt;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	public MemberDAO() {
		//생성자 , 싱글톤 패턴으로 하기 위해 생성자를 private로 설정
		try {
			cnt = new InitialContext();
			ds = (DataSource)cnt.lookup("java:comp/env/jdbc/mysql");
		} catch (Exception e) {
		
		}
		
	}
	
	//싱글톤
	public static MemberDAO getInstance() {
		if(dao == null ) 
			dao = new MemberDAO();
		return dao;
	}
	

	public int insertMember(MemberUser muser) {
		int n = 0;
		
		PreparedStatement pstmt = null;
		String sql = "insert into member"
				+ "(userid, pwd, userirum, useremail, sample4_postcode ,sample4_roadAddress ,userAddress , usertel)"
				+ "values(?,?,?,?,?,?,?,?)";
		

		try {
			con = ds.getConnection();
			System.out.println("con 연결:" + con);
			pstmt = con.prepareStatement(sql);
			
			pstmt.setString(1, muser.getUserid());
			pstmt.setString(2, muser.getPwd());
			pstmt.setString(3, muser.getUserirum());
			pstmt.setString(4, muser.getUseremail());
			pstmt.setString(5, muser.getSample4_postcode());
			pstmt.setString(6, muser.getSample4_roadAddress());
			pstmt.setString(7, muser.getUserAddress());
			pstmt.setString(8, muser.getUsertel());
			
			pstmt.executeUpdate();
			
			n = MemberDAO.MEM_JOIN_SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(pstmt !=null) pstmt.close();
				if(con !=null) con.close();

			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return n;	
	}
	
	//회원아이디로검색
	public int checkid(String userid) {
		int n = 0;
		
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql ="select userid from member where userid = ?";
		
		try {
			con = ds.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, userid);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				n = MemberDAO.MEM_EXIST;
			}else {
				n= MemberDAO.MEM_NONEXIST;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt != null)pstmt.close();
				if(con != null)con.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return n;
	}
	//로그인
	public int memberCheck(String userid, String pwd){
		int n = 0;
		String rPwd;
		
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select pwd from member where userid = ?";
		
		try{
			con = ds.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, userid);
			rs=pstmt.executeQuery();			

			if(rs.next()){
				rPwd = rs.getString("pwd");
				if(rPwd.equals(pwd)){
					n = MemberDAO.MEM_LOGIN_SUCCESS; // 로그인 성공
				}else{
					n = MemberDAO.MEM_LOGIN_PW_NOT; // 비밀번호가 틀림
				}
			}else{
				n = MemberDAO.MEM_LOGIN_NOT; //회원이 아님.
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(rs !=null) rs.close();
				if(pstmt !=null) pstmt.close();
				if(con !=null)con.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return n;		
	}
	
	
	public MemberUser getMember(String userid){
		MemberUser muser = null;
		Connection con = null;
		PreparedStatement pstmt =null;
		ResultSet rs = null;
		String sql = "select * from member where userid = ?";		

		try{
			con = ds.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1,userid);
			rs = pstmt.executeQuery();

			if(rs.next()){

				muser = new MemberUser();
				muser.setUserid(rs.getString("userid"));
				muser.setPwd(rs.getString("pwd"));
				muser.setUserirum(rs.getString("irum"));
				muser.setUseremail(rs.getString("useremail"));
				muser.setSample4_postcode(rs.getString("sample4_postcode"));
				muser.setSample4_roadAddress(rs.getString("sample4_roadAddress"));
				muser.setUserAddress(rs.getString("userAddress"));
				muser.setUsertel(rs.getString("usertel"));
			}			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(rs !=null) rs.close();
				if(pstmt !=null) pstmt.close();
				if(con !=null)con.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return muser; 
	}
	public int updateMember(MemberUser muser){
		int n = 0;
		Connection con = null;
		PreparedStatement pstmt =null;		

		String sql = "update member set userirum=?, useremail=?, userAddress=? where userid=?";	
		try{
			con = ds.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, muser.getUserirum());
			pstmt.setString(2, muser.getUseremail());
			pstmt.setString(3, muser.getUserAddress());
			pstmt.setString(4, muser.getUserid());	
			n=pstmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{				
				if(pstmt !=null) pstmt.close();
				if(con !=null)con.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return n;
	}
	//try catch 안에다가 에러나는 구문을 넣어주거나
	//해당 메서드 명 옆에 throws exception을 붙힘 오류 떠서 예외처리해야됨
	public String Find_id(MemberUser muser) throws Exception {
		String userid = null;
		String sql ="select userid from member where userirum=? and useremail=? and usertel=?";
		try {
			
			con = ds.getConnection();
			pstmt = con.prepareStatement(sql);
			
			pstmt.setString(1, muser.getUserirum());
			pstmt.setString(1,muser. getUseremail());
			pstmt.setString(1, muser.getUsertel());
			
			System.out.println("이름" + muser.getUserid());
			System.out.println("이메일" + muser.getUseremail());
			System.out.println("전번" + muser.getUsertel());;
			
			rs=pstmt.executeQuery();
			
			
			while(rs.next()) {
				userid = rs.getString(userid);
			    
				System.out.println(userid);
			}
		}finally {
			con.close();
			pstmt.close();
			rs.close();			
		}
		return userid;
		
		
	}
	public String Find_pwd(MemberUser muser) throws Exception {
		String pwd =null;
		String sql ="select pwd from member where userirum=? and pwd=? and useremail=?";
		try {
			
			con = ds.getConnection();
			pstmt = con.prepareStatement(sql);
			
			pstmt.setString(1, muser.getUserirum());
			pstmt.setString(2, muser.getUserid());
			pstmt.setString(3, muser.getUseremail());
			rs=pstmt.executeQuery();
			
			while(rs.next()) {
				pwd = rs.getString(pwd);
			    System.out.println(pwd);
			}
		}finally {
			con.close();
			pstmt.close();
			rs.close();			
		}
		return pwd;		
	}

	}

아이디를 찾으면 오류가 뜹니다



HTTP Status 500 – Internal Server Error


Type Exception Report

Message An exception occurred processing [jsp/find_pro_id.jsp] at line [19]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
org.apache.jasper.JasperException: An exception occurred processing [jsp/find_pro_id.jsp] at line [19]

16: MemberDAO dao = MemberDAO.getInstance();
17: 	
18: String userid;
19: userid = dao.Find_id(muser);
20: 
21: String userirum = request.getParameter("userirum");
22: String usertel = request.getParameter("usertel");


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:607)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
javax.servlet.ServletException: java.sql.SQLException: No value specified for parameter 2
	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:667)
	org.apache.jsp.jsp.find_005fpro_005fid_jsp._jspService(find_005fpro_005fid_jsp.java:189)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:458)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
java.sql.SQLException: No value specified for parameter 2
	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
	com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2211)
	com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2191)
	com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2121)
	com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1941)
	org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:122)
	org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:122)
	com.user.MemberDAO.Find_id(MemberDAO.java:240)
	org.apache.jsp.jsp.find_005fpro_005fid_jsp._jspService(find_005fpro_005fid_jsp.java:145)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:458)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.


Apache Tomcat/9.0.12



비밀번호를 찾으면 테이블에 있는 값을 똑같이 입력해도 

찾질 못하네요 

0
0
  • 답변 4

  • black_p
    59
    2019-09-12 18:55:03

    NPE는 NULL을 가진 객체를 사용하려고 했기 때문에 나오는 에러입니다.

    그렇기에 500페이지가 나오네요

    그중에 19번 DAO 쪽에서 에러가 났다고합니다.

    거기서 널값을 받는 부분이 생긴거같은데 로그 찍으면서 확인해보시거나 쿼리문쪽을 확인해보세요

    0
  • 답변 너무 감사합니다 

    null 값을 받는부분을 null을 없앴는데도 불구하고 계속  userid =dao.find_id(muser);쪽에 에러가 나네요,

    java.lang.NullPointerException
    이 쪽에서도 계속 오류가 나고 ㅜㅜ 

    쿼리에 null값이 있긴 한데 

    find에서 가져오는 값이 아닌 값인데도 관련이 있는건가요?

    코딩 배운지 얼마 안돼서 어디가 잘못된건지 잘 모르겠네요,,

    0
  • black_p
    59
    2019-09-12 23:53:49
    애초에 npe에러가 난 시점은 널값을 사용하려 했기 때문에 나는 예외입니다.
    쿼리문에서 널값을 가지고와서 사용하다가 난 에러같네요
    보통 이때 반환값이 문자열이라면 데이터베이스에서 가지고온 객체를 null체크해서 빈 문자열을 넣어주는 방식으로 처리가 가능합니다. 혹은 npe 예외를 잡는 try catch를 이용해 에러메세지를 만들어서 반환해주던가요
    0
  • 귀신님
    259
    2019-09-13 12:54:38

    Find_id 메서드에서

    Sql에 사용되는 파라미터 입력이 모두 1번째 파라미터로 설정되어있네요.


    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.