当前位置:编程学习 > JAVA >>

空指针

源码:
package com.intern.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.intern.Page.Page;
import com.intern.entity.PracticeResultInfo;
import com.intern.entity.TeacherInfo;
import com.intern.jbdc.PracticeResultInfoDAO;
import com.sun.org.apache.regexp.internal.RESyntaxException;

public class PracticeServlet extends HttpServlet {

PracticeResultInfoDAO praDAO=new PracticeResultInfoDAO();

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PracticeResultInfo pra=new PracticeResultInfo();
request.setCharacterEncoding("utf-8");
String oper=request.getParameter("oper");
if(oper.equals("getAll")){
            //实现查询所有学生数据
List<PracticeResultInfo> data1=praDAO.getAll();
//将数据放到request中,转给JSP显示
request.setAttribute("data1", data1);
//转发
request.getRequestDispatcher("/pra_manager.jsp").forward(request, response);
}
if(oper.equals("getOne")){
String strId=request.getParameter("id");
int id=Integer.parseInt(strId);
PracticeResultInfo te=praDAO.getById(id);
//将数据放到requet中,转给JSP显示
request.setAttribute("te", te);
//转发
request.getRequestDispatcher("/pra_show.jsp").forward(request, response);
}
//添加用户信息
if(oper.equals("add")){
PracticeResultInfo userAd=new PracticeResultInfo();
userAd.setStuId(Integer.getInteger(request.getParameter("stuId")));
userAd.setStuName(request.getParameter("stuName"));
userAd.setStuNo(request.getParameter("stuNo"));
userAd.setStuClass(request.getParameter("stuClass"));
userAd.setTeaName(request.getParameter("teaName"));
userAd.setPrTitle(request.getParameter("praTitle"));
userAd.setPrContent(request.getParameter("prContent"));
userAd.setPrFile(request.getParameter("prFile"));
request.setAttribute("userAd", userAd);

if(praDAO.add(userAd)){
request.setAttribute("msg", "添加实习生记录成功!");
}else{
request.setAttribute("msg", "添加实习生记录失败!");
}
request.getRequestDispatcher("/pra_add.jsp").forward(request, response);
}//删除用户信息
if(oper.equals("delete")){
String usId=request.getParameter("id");
int id=Integer.parseInt(usId);
if(praDAO.delete(id)){
request.setAttribute("msg", "删除实习生记录成功!");
}else{
request.setAttribute("msg", "删除实习生记录失败!");
}
request.getRequestDispatcher("/PracticeServlet?oper=getByPage").forward(request, response);
}
//修改用户信息
if(oper.equals("get")){
String usId=request.getParameter("id");
int id=Integer.parseInt(usId);
PracticeResultInfo t=praDAO.getById(id);
request.setAttribute("t", t);
request.getRequestDispatcher("/pra_update.jsp").forward(request, response);
}
if(oper.equals("update")){
pra.setStuId(Integer.parseInt(request.getParameter("stuId")));
pra.setStuName(request.getParameter("stuName"));
pra.setStuNo(request.getParameter("stuNo"));
pra.setStuClass(request.getParameter("stuClass"));
pra.setTeaName(request.getParameter("teaName"));
pra.setPrTitle(request.getParameter("prTitle"));
pra.setPrContent(request.getParameter("prContent"));
pra.setPrFile(request.getParameter("prFile"));
if(praDAO.update(pra)){
request.setAttribute("msg", "修改实习生记录成功!");
request.getRequestDispatcher("/PracticeServlet?oper=getByPage").forward(request, response);
}else{
request.setAttribute("msg", "修改实习生记录失败!");
request.getRequestDispatcher("/PracticeServlet?oper=getByPage").forward(request, response);
}

}
//分页
//按页进行搜所
if(oper.equals("getByPage")){
//默认查询所有数据
String sql="select * from practiceResultinfo order by id";
//定义数据查询集合
List<PracticeResultInfo> allData=null;
//获取查询的参数值
//获取关键字的名称和值
String keywordName=request.getParameter("keywordName");
String keywordValue=request.getParameter("keywordValue");
String orderBy=request.getParameter("orderBy");
//如果查询参数不为空,从新构建sql语句
if(keywordName!=null&&keywordValue!=null){
sql="select * from practiceResultinfo where "+keywordName +" like'%"+keywordValue+"%' ";
    //确定排序规则
    if(orderBy.equals("0")){
sql=sql+" order by id";
}else{
sql=sql+"order by "+orderBy;
}
//将查询数据放到Session中保存
    request.getSession().setAttribute("sql", sql);
}
//判断Session中存在查询条件,进行分页
if(request.getSession().getAttribute("sql")!=null){
sql=(String)request.getSession().getAttribute("sql");
}
//SQL确定后进行分页查询
allData=praDAO.getBySQL(sql);
//判断是否含有页码信息
String currentPageStr=request.getParameter("pageCode");
int currentPage=1;
//如果有页码,则获取页码
if(currentPageStr!=null){
currentPage=Integer.parseInt(currentPageStr);
}
//创建Page对象进行分页
Page p=new Page(allData.size(),currentPage,10);
//得到页面数据
List data=praDAO.getByPage(p,allData);
//将获得的当前页面数据保存到request中
request.setAttribute("data", data);
//将分页信息保存到request中
request.setAttribute("page", p);
//转向
request.getRequestDispatcher("/pra_manager.jsp").forward(request, response);
}
}

}
错误提示:
严重: Servlet.service() for servlet [PracticeServlet] in context with path [/intern] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.apache.jsp.pra_005fmanager_jsp._jspService(pra_005fmanager_jsp.java:124)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at com.intern.servlet.PracticeServlet.doPost(PracticeServlet.java:151)
at com.intern.servlet.PracticeServlet.doGet(PracticeServlet.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
--------------------编程问答-------------------- 你至少把空指针的那一行标注出来啊。! --------------------编程问答-------------------- 根据置顶帖,是不是得直接给你删了。你这像是来问问题的吗。。 --------------------编程问答-------------------- 楼主打断点可以直接看看哪一行出错了,找到空指针位置,问题也就解决75%了。空指针也不是很难处理的问题,一般就是对象为空或没有初始化,检查下就是了。 --------------------编程问答-------------------- 你打断点,自己DEBUG,光看,看不出来 --------------------编程问答-------------------- 楼主啊!错误自己用很显眼的颜色弄出来啊! --------------------编程问答-------------------- 都是代码,看晕了,建议楼主下次要问时应该将出错点指出 --------------------编程问答-------------------- 找到jsp对应的class/java,看124行代码 --------------------编程问答-------------------- at com.intern.servlet.PracticeServlet.doPost(PracticeServlet.java:151)
at com.intern.servlet.PracticeServlet.doGet(PracticeServlet.java:24)

错误给你标识出来了,你自己跟吧。。贴个代码也不贴行数,懒得数行数了
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,