Getting a vector from a bean in a servlet
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ram_Luchman
Posted On:   Saturday, March 30, 2002 09:14 PM

I'm getting nullpointer exception when I pass a resultset turned into vector from a bean to a servlet. I can pass the vector from the bean to a JSP page without problems. My code below. The error happens at this point "rs = (Vector)d.getQueryresult();" package sqlQuery; import java.sql.*; import java.util.Vector; import java.util.Hashtable; public class QueryBean { private String JdbcDrv; private String JdbcDsn; private String DBUser; private String DBPassword; private String query; private Vector v; private Connection con; private ResultSet rs; private Statement st; public QueryBean()    More>>

I'm getting nullpointer exception when I pass a resultset turned into vector from a bean to a servlet. I can pass the vector from the bean to a JSP page without problems. My code below. The error happens at this point "rs = (Vector)d.getQueryresult();"

			
package sqlQuery;

import java.sql.*;
import java.util.Vector;
import java.util.Hashtable;

public class QueryBean
{
private String JdbcDrv;
private String JdbcDsn;
private String DBUser;
private String DBPassword;
private String query;
private Vector v;
private Connection con;
private ResultSet rs;
private Statement st;


public QueryBean()
{
JdbcDrv = "";
JdbcDsn = "";
DBUser = "";
DBPassword = "";
query = "";
con = null;
rs = null;
st = null;
v = null;
}

public void setDBDriver(String dr)
{
JdbcDrv = dr;
}

public void setDBDsn(String ds)
{
JdbcDsn = ds;
}

public void setSQLquery(String sq)
{
query = sq;
}

public Vector getQueryresult()
{
try
{

Class.forName(JdbcDrv);
con = DriverManager.getConnection(JdbcDsn, DBUser, DBPassword);

st = con.createStatement();
rs = st.executeQuery(query);

v = new Vector();
ResultSetMetaData meta = rs.getMetaData();

while(rs.next())
{
Hashtable row = new Hashtable();
for (int x = 1; x <= meta.getColumnCount(); x++)
{
String column = meta.getColumnName(x);
String value = rs.getString(column);
if (value == null || value == "")
{
value="";
}
row.put(column,value.trim());
}
v.add(row);
}

}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (rs != null)
{rs.close();}
if (st != null)
{st.close();}
if (con != null)
{con.close();}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
v.trimToSize();
return v;
}

}

#### my servlet here ####

package baseServlets;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Vector;
import sqlQuery.QueryBean;


public class Login extends HttpServlet
{
public void doPost (HttpServletRequest request, HttpServletResponse response)
{
Vector rs = new Vector();
String drv = getServletContext().getInitParameter("jdbcDriver");
String dsn = getServletContext().getInitParameter("jdbcDsn");
String q;
String userName = request.getParameter("userName");
String password = request.getParameter("password");
QueryBean d = new QueryBean();

try
{
q = "SELECT * FROM Employees";
q += "WHERE userID = '" + userName + "' AND password = '" + password + "'";


d.setDBDriver(drv);
d.setDBDsn(dsn);
d.setSQLquery(q);

rs = (Vector)d.getQueryresult();
}
catch (Exception e)
{
log("

Error: ", e);
e.printStackTrace();
}

try
{
if (rs.isEmpty())
{
request.getRequestDispatcher("/index.jsp?logfail=1").
forward(request, response);
}
else
{
request.getRequestDispatcher("/test.jsp").
forward(request, response);
}

}
catch (Exception e)
{
log("

Redirect Error: ", e);
e.printStackTrace();
}


}


public void doGet (HttpServletRequest request, HttpServletResponse response)
{
doGet(request, response);
}


}
   <<Less

Re: Getting a vector from a bean in a servlet

Posted By:   Bozidar_Dangubic  
Posted On:   Monday, April 1, 2002 09:43 AM

first, you do not need to cast to Vector because the QueryBean is returning Vector. second, never create an object just to have a variable of certain type. this is what you did

Vector rs = new Vector();
......
rs = (Vector)d.getQueryresult();

when you invoked Vector rs = new Vector(); you created a Vector object that was never used and you wasted memory. Never do that. finally, a question for you. did QueryBean correctly executed the statement and got results. printout the Vector in QueryBean before returning it to the servlet and see if you are even getting the data from the database in the first place. most likely that is where the problem is. there is no reason why you would be getting NullPointerException other than the fact that your query hasn't executed correctly in the first place. so check that...
About | Sitemap | Contact