0

I am using a servlet and jsp pages to display database information in a table. I can successfully insert info to the database but when I try to display the information in another page I am getting an issue. I get an error in the for(itr = data.iterator(); itr.hasNext();) line. My code is right below and the errors I am getting are below my code. I know my main issue is iterating so if someone could help I would really appreciate it. (Btw, this code is word for word off a youtube video I was watching where the user got it to work just fine).

<%@page import="java.util.Iterator" %>
<%@page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Scheduling</title>
</head>
<body>
    <table>
    <%Iterator itr;%>
    <%List data = (List)request.getAttribute("jobsData");
    for(itr = data.iterator(); itr.hasNext();){
        %>
    <tr>
        <% String s = (String) itr.next();%>
        <td><%=s%></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><%= itr.next() %></td>
        <td><input type="submit" value="Edit" name="edit" onclick ="editRecord(<%=s%> %>);"></td>
        <td><input type="submit" value="Delete" name="delete" onclick ="deleteRecord(<%=s%> %>);"></td>
        <%} %>
        </tr>
</table>

Error:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

root cause

java.lang.NullPointerException
org.apache.jsp.displayjobs_jsp._jspService(displayjobs_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

UPDATE: Here is my servlet code.

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class displayjobs
 */
@WebServlet("/schedule")
public class schedule extends HttpServlet {
List list = new ArrayList();
Statement stmt;
ResultSet res;
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public schedule() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
        String connectionUrl = "jdbc:sqlserver://10.11.1.246;databaseName=Test;integratedSecurity=false;user=sa;password=S0l1dConcepts";
        Connection con = DriverManager.getConnection(connectionUrl);
        String query = "select * from jobs";
        stmt = con.createStatement();
        res = stmt.executeQuery(query);
        while(res.next()){
            list.add(res.getString(1));
            list.add(res.getString(2));
            list.add(res.getString(3));
            list.add(res.getString(4));
            list.add(res.getString(5));
            list.add(res.getString(6));
            list.add(res.getString(7));
            list.add(res.getString(8));
            list.add(res.getString(9));
            list.add(res.getString(10));
        }
        res.close();
    }
    catch(Exception e){
        RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
        rd.forward(request, response);
    }
    finally{
        request.setAttribute("jobsData",list);
        RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
        rd.forward(request, response);
        list.clear();
        out.close();
    }

}

}
user3772999
  • 59
  • 1
  • 8
  • 3
    Easy. `data` is `null`. While you search why the attribute is not found (wrong name?), think about leaving scriptlet and using JSTL. – SJuan76 Jul 28 '14 at 22:28
  • it seems "List data" have null pointer exception. (List)request.getAttribute("jobsData"); is have a result for you? Write "data" for us – hurricane Jul 28 '14 at 22:28
  • why is data null? I am using the right name for attribute. I will include my servlet code if that helps. I was unable to just use data. The list cast is necessary for the code to work. – user3772999 Jul 28 '14 at 22:49
  • make sure our are getting the data from the database and the list u are forwarding is not null – SparkOn Jul 29 '14 at 17:16
  • the thing is I tried doing it with a list of one string "hello" and it is still null. I am assuming that the setAttribute is the issue but don't know where to go from here. – user3772999 Jul 29 '14 at 17:27
  • You can upvote/accept the answers! – instanceOfObject Jul 30 '14 at 06:55

2 Answers2

0

In the jsp you are trying to obtain the value of "jobsData" and in the servlet you are storing the list in "jData". Due to that reason the request attribute can not be obtained and data is set to null

rafaborrego
  • 610
  • 1
  • 8
  • 19
  • I apologize. I changed it so both match to jobsData. I am still getting the null pointer exception. Any idea why this is? – user3772999 Jul 29 '14 at 15:45
  • For some reason, request.getAttribute(jobsData) is still returning null when both names match. I thought the list might have been the issue so I try setting jobsData equal to 10. After casting request.getAttribute(jobsData) to Integer, it is still returning null. Any idea why? – user3772999 Jul 29 '14 at 16:16
  • The first time that a page is loaded it is done with a GET petition, and after clicking on submit you can do it with a GET or a POST one. You only add the list at the doPost method, so the list is not added the first time that you load the page. Try adding the same code to the doGet method – rafaborrego Jul 29 '14 at 20:39
  • I tried adding it to the doGet method. Now I am getting: java.lang.IllegalStateException: Cannot forward after response has been committed. Any idea why this is? – user3772999 Jul 30 '14 at 19:30
  • If an exception is thrown, it does two times the forward, once inside the catch block and another time inside the finally block. So when you do the second forward the response has alreay been commited. Please remove the forward from the catch and try again. – rafaborrego Jul 30 '14 at 23:58
  • I did this and I am still having issues. When I try running the servlet page, it is just blank. I figured out that the NPE is a result of running the jsp page instead of the servlet page. The jsp gets the info from the servlet page and unless the servlet runs, the JSP won't. Any idea why it is blank? – user3772999 Jul 31 '14 at 19:06
  • Please, could you add ` – rafaborrego Aug 05 '14 at 10:58
0
1    List data = (List)request.getAttribute("jobsData");
2    for(itr = data.iterator(); itr.hasNext();

First line from the copied code returns a NULL list because name of the attribute in the servlet has been set by a different name (if your intention is to pass "list" as "jobsData") i.e. "jData".

 finally{
        request.setAttribute("jData",list);
        RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
        rd.forward(request, response);
        list.clear();
        out.close();
    }

You need to fix this naming to make it work.

Edit 1:

Your code blocks are called at different point of time. GET is called on page load and POST is called on form submission. You are getting NPE because this block is not doing anything.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

Please go through doGet and doPost in Servlets to check the basics. Block you are referring to is a POST block. You need to pre-populate the same object in get block to pass the data on page load.

Community
  • 1
  • 1
instanceOfObject
  • 2,936
  • 5
  • 49
  • 85
  • I apologize. I changed it so both match to jobsData. I am still getting the null pointer exception. Any idea why this is? – user3772999 Jul 29 '14 at 15:44
  • For some reason, request.getAttribute(jobsData) is still returning null when both names match. I thought the list might have been the issue so I try setting jobsData equal to 10. After casting request.getAttribute(jobsData) to Integer, it is still returning null. Any idea why? – user3772999 Jul 29 '14 at 16:15
  • I tried adding it to the doGet method. Now I am getting: java.lang.IllegalStateException: Cannot forward after response has been committed. Any idea why this is? – user3772999 Jul 30 '14 at 19:30
  • look at @rafaborrego's response! – instanceOfObject Jul 31 '14 at 18:16