gpt4 book ai didi

java - 当我创建新数据时参数应该为空时,我不断收到 null 异常

转载 作者:行者123 更新时间:2023-12-02 09:50:10 24 4
gpt4 key购买 nike

我的 servlet 中的 doPost 方法遇到问题。我正在用java和oracle为其数据库做一个简单的系统。而且在数据库中,属性rph_id是自增的,所以在Java中,当我想创建一组新数据时,rph_id应该为空,对吗?

这些代码以前有效,但现在不再有效。它不断给出空异常。

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

int rph_id = Integer.parseInt(request.getParameter("rph_id"));
String rph_date = request.getParameter("rph_date");
String rph_time = request.getParameter("rph_time");
String rph_type = request.getParameter("rph_type");
int teacher_id = Integer.parseInt(request.getParameter("teacher_id"));
int subject_id = Integer.parseInt(request.getParameter("subject_id"));
String rph_submitstatus = null;
String rph_comment = null;

RPHS rph = new RPHS(rph_date, rph_time, rph_type, rph_submitstatus, rph_comment, teacher_id, subject_id, false);
rph = RphDAO.getRph(rph);
}

我在行 int rph_id = Integer.parseInt(request.getParameter("rph_id"));

rph_id在数据库中是自动递增的。这是父类中的一个属性,我必须在子类中使用。

最佳答案

你说它是数据库中的自增键,那么你为什么要尝试从请求参数中获取它?如果您从未设置过值或通过 html 表单等设置过,则无法从那里获取值。

插入后,您应该从 RPHS 对象中获取它。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//int rph_id = Integer.parseInt(request.getParameter("rph_id"));
..
..

RPHS rph = new RPHS(rph_date, rph_time, rph_type, rph_submitstatus, rph_comment, teacher_id, subject_id, false);
rph = RphDAO.getRph(rph);

int rph_id=rph.getRphId();
}

关于java - 当我创建新数据时参数应该为空时,我不断收到 null 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com