gpt4 book ai didi

java - for 每个循环中的空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:31 25 4
gpt4 key购买 nike

我正在 JSP 中开发电影租赁应用程序。在复选框的帮助下选择电影后,当我迭代它时,它给了我空指针异常。以下是 Statement.jsp 文件的代码,该文件正在为租借的电影创建声明...

<%@page language="java" %>
<%@page import="java.sql.*, java.util.*, mrs.*" %>
<html>
<head>
<title>Movie Rental Application</title>
</head>
<body>
<%@include file="Connection.jsp" %>

<% if (session.getAttribute("Name") != null) {
String[] checkboxes = null;
String[] days = null;

checkboxes = request.getParameterValues("c1");
days = request.getParameterValues("t1");
int i = 0;
for (String title : checkboxes) {
String Name = session.getAttribute("Name").toString();
int code = 0;
ResultSet rs = stmt.executeQuery("select Type from Movies where Title = '" + title + "'");
String Type = rs.toString();
if (Type.equalsIgnoreCase("REGULAR")) {
code = 0;
}
if (Type.equalsIgnoreCase("NEW RELEASE")) {
code = 1;
}
if (Type.equalsIgnoreCase("CHILDREN")) {
code = 2;
}
mrs.Movie m = new mrs.Movie(title, code);
int day = Integer.parseInt(days[i]);
i++;
mrs.Rental r = new mrs.Rental(m, day);
mrs.Customer c = new mrs.Customer(Name);
c.addRental(r);
stmt.executeQuery("insert into rents values('" + day + "','" + Name + "','" + title + "')");
String result = c.statement();
out.println(result);
}
stmt.close();
con.close();
%>
<% }%>
</body>
</html>

这是异常...

最佳答案

您没有检查该复选框是否已选中。

 checkboxes = request.getParameterValues("c1");
days = request.getParameterValues("t1");
int i = 0;
if(checkboxes!=null){
for (String title : checkboxes) {
if(title.equals("on")){
// your code
}
}

您可以引用以下link根据提到的条件检查复选框返回哪个值。

关于java - for 每个循环中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359371/

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