Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

Servlet 报错 java.lang.NullPointerException

Servlet 报错 java.lang.NullPointerException

一个由Tomcat部署的数据库Web程序

代码是老师提供的,同学用eclipse跑起来都正常,但是我用IDEA出问题了。

下面的表单POST之后,就报错

java.lang.NullPointerException
com.action.library.LoginAction.memberLogin(LoginAction.java:12)
com.controller.library.LoginServlet.service(LoginServlet.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


<form action="servlet/LoginServlet" method="POST">
    <table>
        <tr>
            <td>Member Number:</td>
            <td><input type="text" name="member_no"></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
    </table>
</form>

相关的代码片段如下

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = -8052223741830167994L;

    public LoginServlet() {
    }

    public void service(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException var10) {
            var10.printStackTrace();
        }

        String memberNo = request.getParameter("member_no");
        String password = request.getParameter("password");
        LoginAction login = new LoginAction();
        Member member = login.memberLogin(memberNo, password);
        if (member == null) {
            try {
                response.sendRedirect("/library/login_incorrect.html");
            } catch (IOException var9) {
                var9.printStackTrace();
            }
        } else {
            request.getSession().setAttribute("member", member);

            try {
                response.sendRedirect("/library/servlet/RefreshMainPageServlet");
            } catch (IOException var8) {
                var8.printStackTrace();
            }
        }

    }
}
public class LoginAction {
    public LoginAction() {
    }

    public Member memberLogin(String memberNo, String password) {
        DaoManager dm = DaoManager.getInstance();
        MemberDao memberDao = (MemberDao)dm.getDao(MemberDao.class);
        Member member = null;

        try {
            dm.begin();
            member = memberDao.getMemberByMemberNoAndPassword(memberNo, password);
            dm.commit();
        } catch (Exception var10) {
            var10.printStackTrace();
        } finally {
            dm.end();
        }

        return member;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

image.png
这里看看


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...