1

I am Developing Movie Rental Application in JSP. After selecting movie with the help of checkbox, when I am iterating with that it is giving me null pointer exception. Here is the code for Statement.jsp file which is creating statement for the rented movies...

<%@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>

Here it is the Exception...

Ravi
  • 30,829
  • 42
  • 119
  • 173
Divyani Garg
  • 138
  • 1
  • 3
  • 15

1 Answers1

0

You are not checking whether the checkbox is checked or not.

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

You may refer following link to check which value is returned by the checkbox based on the conditions mentioned.

Radhika Kulkarni
  • 298
  • 1
  • 4
  • 19