Hello Everyone,

I am a new java user and i have a problem where i have made a servlet "Conn.java".
In which i have established a connection and i m retrieving data from database using dsn.

This data i store in a array list called rowsetArray.
when i run just the servlet with " out.println("<h1>name======" + rowsetArray + "</h1>"); "
statement in it; it displays all the records in the arraylist on the explorer.

but when i uncomment the above line and try to pass the array list data to my index.jsp then i am
unable to do so.
I get null pointer exception error.

below is my servlet code and follwed by index.jsp code.

---------------------------------Conn.java--------------

package IRIS;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.String;
import java.sql.*;
import java.util.*;
 import javax.servlet.*;

/**
 *
 * @author vishal
 * @version
 */
public class Conn extends HttpServlet {
    
  
    public Conn()
    {
        
    }

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
            ArrayList rowArray = new ArrayList(); 
            ArrayList rowsetArray = new ArrayList(); 
            Connection con;
            PreparedStatement ps;
            Statement stmt=null;
            ResultSet rs;
            String name="";
            String colName="";
            ResultSet rs1=null;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Connection Estabished");
            con=DriverManager.getConnection("Jdbc:Odbc:dsn_admin","sa","sa");
            System.out.println("Connection Estabished1");
            stmt=con.createStatement();
            //ResultSetMetaData rsmd=null;
            rs1=stmt.executeQuery("select * from employee");
          


        while(rs1.next())
            {
              rowArray.clear(); 
              String str = rs1.getString("FirstName");
              rowArray.add(str);  
              rowsetArray.add(rowArray.clone()); 

              
            }
        request.setAttribute("myArrayList",rowsetArray);
        RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("/Prism/web/index.jsp");
	requestDispatcher.forward(request,response);
        

    	}
    	catch(Exception e)
    	{
        System.out.println(e);
        e.printStackTrace();
	
    	}
        //out.println("<h1>name======" + rowsetArray + "</h1>");
        
        //return name;
       // out.close();
    }

note i have not mentioned the httpservlet methods here but they are there in my code.
i am using netbeans 5.0......................

-------------------------------------------Code for index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="IRIS.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>




<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

    <h1></h1>
    
   <!--/ <img src="Images/Logo_Final Splash.gif" width="450" height="200" alt="Logo_Final Splash"/>
   -->
  
     <table border="1">
          <tr><td><B>Emp Names</B></td></tr>
                   
                        
    <% 
    ArrayList pageArray = (ArrayList) request.getAttribute("myArrayList");  
                        String myString="";
                        if(pageArray.isEmpty()==false)
                        {
                        for(int i = 0; i < pageArray.size(); i++) 
                        { 
                         myString = (String) pageArray.get(i); 
                        }
                        }
                        else
                        {
                         System.out.println("Array is empty");   
                        }
                        %> 
    
                  <tr><td><%=myString%></td></tr> 
                  
                    
    </table>

    
    </body>
</html>

---------------------------------below is the error I get........

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

root cause

java.lang.NullPointerException
	org.apache.jsp.index_jsp._jspService(index_jsp.java:81)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.9


-----------Please let me know what is wrong.plsssssssssssss
I want to learn java to the best,,,please help me.........

You ought to get a BIG RED for the amount of PMs you sent me without even making a single thread here, And finally when you post you do not use code tags !!!:angry: :angry: :angry:

  1. Sending personal messages to members asking for private immediate help is not welcomed
  2. Why you using ODBC driver instead of JDBC?
  3. This line
    RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/Prism/web/index.jsp");

    should be

    RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/index.jsp");

    if I'm not mistaken

I made changes to my Conn.java servlet as suggested by you.

still i get null values in jsp page.

The line
out.println(pageArray.get(i)); gives 1 as output on jsp page where as
same line in my servlet clas gives size as 110 which is the true value.

why am i not being able to pass my array data to jsp page.

Sorry for the private messages as i was not knowing how to start a thread and i was frustated with my code by mistae i send you pm's.

Sorry again for sending you my code again, as there have ben minor modifications to it.

package IRIS;

import java.io.*;
import java.net.*;


import javax.servlet.http.*;
import java.lang.String;
import java.sql.*;
import java.util.*;
import javax.servlet.*;

/**
*
* @author vishal
* @version
*/
public class Conn extends HttpServlet {


public Conn()
{

}


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ArrayList rowArray = new ArrayList(); 
ArrayList rowsetArray = new ArrayList(); 
Connection con;
PreparedStatement ps;
Statement stmt=null;
ResultSet rs;
String name="";
String colName="";
ResultSet rs1=null;
try
{
    String str[]=new String[200];
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Connection Estabished");
    con=DriverManager.getConnection("Jdbc:Odbc:dsn_admin","sa","sa");
    System.out.println("Connection Estabished1");
    stmt=con.createStatement();
    rs1=stmt.executeQuery("select * from employee");
     System.out.println("<tr><td>name</td></tr>");
     int n =rs1.getRow();
    while(rs1.next())
    {
        str[n] = rs1.getString("FirstName");
        rowArray.add(str[n]); 
        //out.println("<tr><td><br></br>" + rowArray + "</td></tr>");
        n++;
    }
    for (int i=0;i<rowArray.size();i++)
    {
        //out.println("<tr><td><br>name======" + rowArray.get(i) + "</td></tr>");
    request.getSession().setAttribute("myArrayList",rowArray);
    RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("/index.jsp");
    requestDispatcher.forward(request,response);
    }
}
catch(Exception e)
{
    System.out.println(e);
    e.printStackTrace();

}


//return name;
// out.close();
}
  //----http methods.....m using net beans 5.0.  
}

--------------------------------

Index.jsp code.

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="IRIS.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>


<h1></h1>

<!--/ <img src="Images/Logo_Final Splash.gif" width="450" height="200" alt="Logo_Final Splash"/>
-->



 <table border="1">
 <tr><td><B>Emp Names</B></td></tr>
            <% 
            try
            { 
                String myString ="";
                ArrayList pageArray = new ArrayList();
                pageArray.add(request.getSession().getAttribute("myArrayList"));
                //pageArray.add(request.getAttribute("myArrayList")); 
               for (int i=0;i<pageArray.size();i++)
                {
                    out.println(pageArray.get(i));
                }
                out.println(pageArray.size());
            }
            catch(Exception e)
            {
                out.println(e);
                e.printStackTrace();
            }
            %>
</table>





</body>
</html>

..................please let me know why out.println(pageArray.get(i)); dont reflet the actual size 110.....i think it is sending the last row of array data.......or arraylist as 1 string maybe...please help...

What would be interesting to know is how do you start this whole thing? Why I ask? Because the JSP to which you forward these data from servlet is called index.jsp, which by common logic is starting point of any website (extension may be different html/php/asp depending on used technology). If you call this page first there is no way it will get any data from your servlet. Is there any other page that actually call this servlet? Or how do you run it?

No Sir there's just 1 Conn.java file i.e my servlet and index.jsp file in my project.

The composition of my projet is as follows.

1. project name "Prism" which has Package IRIS .
and Conn.java in which I have established the connection through dsn.
and I store the result set in Arraylist which I am trying to send the arraylist to my jsp page.

It seems a simple program to learn jsp/servlet as a beginner which i m not able to do.....

What would be interesting to know is how do you start this whole thing?

Are u going directly to your jsp page or your servlet first

I suggest calling your servlet which will then redirect to the jsp page, then your array should be filled.

Are u going directly to your jsp page or your servlet first

I suggest calling your servlet which will then redirect to the jsp page, then your array should be filled.

  1. It is not me who need help so I do not know why did you quote me...
  2. It is obvious that there have to be something before servlet, even simple html document with redirect to servlet to solve the issue

I know u dont need the help, I quoted u, cause u had already asked what i wanted to and he/she didnt answer that question. As vish_1x1 pointed out, he is new to java, and what might be obvious to one person is not always obvious to another person.

This article has been dead for over six months. Start a new discussion instead.