javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause java.lang.NullPointerException
src.Gone.NewsEditSubmitActiong.execute(NewsEditSubmitActiong.java:50)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)package src.Gone;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.validator.DynaValidatorForm;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;public final class NewsEditSubmitActiong extends Action{  
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,  
HttpServletResponse response) throws Exception {

    DynaValidatorForm newsgForm = (DynaValidatorForm) form;         
Integer gongyingId = (Integer)newsgForm.get("gongyingId");
String title = (String)newsgForm.get("title");
String content = (String)newsgForm.get("content");
String author = (String)newsgForm.get("author");
String keyword = (String)newsgForm.get("keyword");
Integer gongyingType = (Integer)newsgForm.get("type");

        HttpSession session = request.getSession();
Vector newsList = new Vector();

    ServletContext context = servlet.getServletContext();
DataSource dataSource = 
(DataSource)context.getAttribute(Constantsg.DATASOURCE_KEY);

        DB db = new DB(dataSource);
String PageForward;
        
Newsg gongying=new Newsg();
gongying.setId(gongyingId.intValue());第50行
gongying.setTitle(title);
gongying.setContent(content);
gongying.setAuthor(author);
gongying.setKeyword(keyword);
gongying.setType(gongyingType.intValue());

ActionMessages errors = new ActionMessages();
if (gongying.Edit(db)){
newsList = Newsg.SearchNewsTitle(db);
session.setAttribute(Constantsg.GONG_LIST_KEY,newsList);
PageForward="toAdminMain";
}
else{
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                           new ActionMessage("errors.updateFail"));
if (!errors.isEmpty()) {
saveErrors(request, errors);

  PageForward="toWrong";  
}
   
db.close();
    return (mapping.findForward(PageForward));
  }
}添加删除都可以,数据库应该没有问题,就这个编辑不行,请高手们给看下错在哪里了,3Q了先!!!

解决方案 »

  1.   

    gongying.setId(gongyingId.intValue());第50行 
    id  在库里不是自增的吧....
      

  2.   

    你的Newsg类和本文件是在同一个包下吗?如果不是,请用“import"导入!对了,还有你的DB类!
      

  3.   

    gongyingId打印出来看看啊 是不是null
      

  4.   

    gongyingId是NULL的。
    Integer gongyingId = (Integer)newsgForm.get("gongyingId"); 加个断点试试!
      

  5.   

    Integer gongyingId = (Integer)newsgForm.get("gongyingId"); 
    确认这里不为空么?
      

  6.   

    Integer gongyingId = (Integer)newsgForm.get("gongyingId"); 
    这里从 Form获取的gongyingId 开始的时候是空的吗?如果上面获取的是 null ,那这里就会出现 Integer gongyingId = (Integer)newsgForm.get("gongyingId");  
    的NullPointerException 
    异常。所以要在上面获取的时候就判断是否从空的获取得到的 gongyingId /这里要判断下
    if(newsgForm.get("gongyingId")==null){
       你需要的语句,处理下
    }else{
      Integer gongyingId = (Integer)newsgForm.get("gongyingId"); 
    }