怎么空指针异常了。。

怎么空指针异常了。。

这个Mmessage怎么异常了,而且也没有获取到user的值

java.lang.NullPointerException

at com.xing.dao.impl.UserDaoImpl.Mmessage(UserDaoImpl.java:139)

at com.xing.service.impl.UserServiceImpl.Mmessage(UserServiceImpl.java:41)

at com.xing.web.action.UserServlet.Mmessage(UserServlet.java:44)

at com.xing.web.action.UserServlet.doGet(UserServlet.java:30)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)


package com.xing.dao.impl;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;


import com.xing.dao.UserDao;

import com.xing.domain.Message;

import com.xing.domain.User;

import com.xing.util.JDBCUtils;


public class UserDaoImpl implements UserDao {


@Override

public User login(User user) {

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rl = null;

try {

conn = JDBCUtils.getConnection();

String sql = "select * from user where username=? and password=?";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, user.getUsername());

pstmt.setString(2, user.getPassword());

rl = pstmt.executeQuery();

if(rl.next()) {

User muser = new User();

muser.setId(rl.getInt("id"));

muser.setUsername(rl.getString("username"));

muser.setPassword(rl.getString("password"));

muser.setReal_name(rl.getString("real_name"));

muser.setBirthday(rl.getDate("birthday"));

muser.setPhone(rl.getString("phone"));

muser.setAddress(rl.getString("address"));

return muser;

}

} catch (Exception e) {

e.printStackTrace();

}finally {

JDBCUtils.release(rl, pstmt, conn);

}

return null;

}


@Override

public void reg(User user) {

Connection conn = null;

PreparedStatement pstmt = null;

try {

conn = JDBCUtils.getConnection();

String sql = "insert user set username=?,password=?";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, user.getUsername());

pstmt.setString(2, user.getPassword());

pstmt.executeUpdate();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

JDBCUtils.release(pstmt, conn);

}

}


@Override

public User checkuname(String username) {

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rl = null;

try {

conn = JDBCUtils.getConnection();

String sql = "select * from user where username=?";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, username);

rl = pstmt.executeQuery();

if(rl.next()) {

User user = new User();

user.setId(rl.getInt("id"));

user.setUsername(rl.getString("username"));

user.setPassword(rl.getString("password"));

user.setReal_name(rl.getString("real_name"));

user.setBirthday(rl.getDate("birthday"));

user.setPhone(rl.getString("phone"));

user.setAddress(rl.getString("address"));

return user;

}

} catch (Exception e) {

e.printStackTrace();

}finally {

JDBCUtils.release(rl, pstmt, conn);

}

return null;

}


@Override

public List<Message> mymessage(User muser) {

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rl = null;

List<Message> mlist = new ArrayList<Message>();

try {

conn = JDBCUtils.getConnection();

String sql = "select * from message as m "

+ "join user as u on m.user_id = u.id where m.user_id=?";

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, muser.getId());

rl = pstmt.executeQuery();

while(rl.next()) {

Message message = new Message();

message.setId(rl.getInt("id"));

message.setUser_id(rl.getInt("user_id"));

message.setUsername(rl.getString("username"));

message.setTitle(rl.getString("title"));

message.setContent(rl.getString("content"));

message.setCreate_time(rl.getDate("create_time"));

mlist.add(message);

}

} catch (Exception e) {

e.printStackTrace();

}finally {

JDBCUtils.release(rl, pstmt, conn);

}

return mlist;

}


@Override

public User Mmessage(Integer id) {

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rl = null;

try {

conn = JDBCUtils.getConnection();

String sql = "select * from user where id = ?";

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, id);

rl = pstmt.executeQuery();

if(rl.next()) {

User user = new User();

user.setId(rl.getInt("id"));

user.setUsername(rl.getString("username"));

user.setPassword(rl.getString("password"));

user.setReal_name(rl.getString("real_name"));

user.setBirthday(rl.getDate("birthday"));

user.setPhone(rl.getString("phone"));

user.setAddress(rl.getString("address"));

return user;

}

} catch (Exception e) {

e.printStackTrace();

}finally {

JDBCUtils.release(rl, pstmt, conn);

}

return null;

}


}



<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>

    <head>

        <meta charset="UTF-8">

        <title>我的留言</title>

        <link rel="stylesheet" href="${pageContext.request.contextPath }/css/index.css">

        <link rel="stylesheet" href="${pageContext.request.contextPath }/css/bootstrap.min.css">

        <script type="text/javascript">

            

        </script>

    </head>


    <body>

        <header>

            <div class="container">

                <% if (null != request.getSession().getAttribute("muser")) {%>

                    <nav>

                        <a href="${pageContext.request.contextPath }/MessageServlet?method=mymessage">我的留言</a>

                    </nav>

                    <nav>

                        <a href="${pageContext.request.contextPath }/UserServlet?method=Mmessage&id=${muser.id}">我的信息</a>

                    </nav>

                <%} else { %>

                    <nav>

                        <a href="">登录</a>

                        <a href="">注册</a>

                    </nav>

                <% } %>

            </div>

        </header>

        <section class="banner">

            <div class="container">

                <div>

                    <h1>慕课网留言板——我的留言</h1>

                    <p>慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人,也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。 </p>

                </div>

            </div>

        </section>

        <c:forEach items="${pagebean.list }" var="m">

        <section class="main">

            <div class="container">

                

                    <div class="alt-item">

                        <div class="alt-head">

                            <div class="alt-info">

                                <span>作者:${m.username }<a href=""></a></span>

                                <span>时间:${m.create_time }</span>

                            </div>

                        </div>

                        <div class="alt-content">

                            <h3>${m.title }</h3>

                            <p>${m.content }</p>

                        </div>

                        <div align="right">

                            <table>

                                <tr><td>   

                                        <button class="btn btn-primary" type="submit">修改</button>                                

                                </td>

                                <td>

                                        <button class="btn btn-primary" type="submit">删除</button>                                  

                                </td></tr>

                            </table>

                        </div>

                    </div>

             

            </div>

        </section>

        </c:forEach>

        <section class="page">

             <div class="container" id="pagefy">

         <ul>

<li><a href="${pageContext.request.contextPath }/MessageServlet?method=mymessage&&page=1">首页</a></li>

<li><a href="${pageContext.request.contextPath }/MessageServlet?method=mymessage&&page=${pagebean.page-1}">上一页</a></li>

<li><a href="#">当前第${pagebean.page }页</a></li>

<li><a href="${pageContext.request.contextPath }/MessageServlet?method=mymessage&&page=${pagebean.page+1}">下一页</a></li>

<li><a href="${pageContext.request.contextPath }/MessageServlet?method=mymessage&&page=${pagebean.totalpage}">尾页</a></li>

</ul>

           <!-- 分页内容参考视频中老师源码 -->

            </div>

        </section>

        <footer>

            copy@慕课网

        </footer>

    </body>

</html>




package com.xing.web.action;


import java.io.IOException;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.xing.domain.Message;

import com.xing.domain.User;

import com.xing.service.UserService;

import com.xing.service.impl.UserServiceImpl;


/**

 * Servlet implementation class UserServlet

 */

    @WebServlet("/UserServlet")

    public class UserServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String method = request.getParameter("method");

if(method.equals("login")) {

login(request,response);

}else if(method.equals("Mmessage")){

Mmessage(request,response);

}

}


/**

* 用户信息跳转

* @param request

* @param response

* @throws IOException 

* @throws ServletException 

*/

private void Mmessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Integer id = Integer.getInteger(request.getParameter("id"));

UserService us = new UserServiceImpl();

User user = us.Mmessage(id);

System.out.println("user:"+user);

request.setAttribute("user", user);

request.getRequestDispatcher("/user.jsp").forward(request, response);

}



private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String yzm = request.getParameter("checkcode");

String yzm1 = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

if(yzm==null || !yzm.equalsIgnoreCase(yzm1)) {

request.setAttribute("msg", "验证码输入不正确");

request.getRequestDispatcher("/login.jsp").forward(request, response);

return;

}

String username = request.getParameter("username");

String password = request.getParameter("password");

User user = new User();

user.setUsername(username);

user.setPassword(password);

UserService us = new UserServiceImpl();

User muser = us.login(user);

if(muser==null) {

request.setAttribute("msg", "用户未登录或者密码错误");

request.getRequestDispatcher("/login.jsp").forward(request, response);

}else {

request.getSession().setAttribute("muser", muser);

request.getRequestDispatcher("/MessageServlet?method=findAll").forward(request, response);

}

}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}


}





正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好!

建议你找到项目在tomcat上部署的位置,删除,然后重新启动项目测试一下

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

好帮手慕柯南 2019-12-24 18:20:49

同学你好!

老师这里测试你的代码,没有报错,可能是项目没有编译好,建议你clean一下项目,重启项目测试一下。如果还报错,请问同学报错的类中139行的代码是什么呢?

http://img1.sycdn.imooc.com//climg/5e01e64c09e2a79207100215.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 邢文凯888 #1
    pstmt.setint(1,id) 是这个,我奇怪也没有什么错误呀,为什么会空指针异常
    2019-12-24 18:27:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师