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.........