JavaServer Pages (JSP) and JSTL - jsp is throwin "java.lang.NullPointerException" while using Re

hi..

please help .

i have one jsp page "processRegistration.jsp". which was called thru another jsp...below is the code

<%@ page language="java" %>

<%@ page import="DataAccessObject.Connect" %>

<%@ page import="java.sql.*"%>

<%@ page import="javax.sql.*" %>

<html>

<body>

<%

String str_rid = (String)request.getParameter("ru");

int user_rid=Integer.parseInt(str_rid);

Connect.connectDb();

String str ="select * from Registration where rid=user_rid";

ResultSet rs=Connect.processQuery(str);

String fname;

if(rs.next())

{

fname=rs.getString(2);

out.println(fname);

}

Connect.closeConnection();

}

%>

</body>

</html>

when i run i get exception "The server encountered an internal error() that prevented it from fulfilling this request"

root cause : java.lang.NullPointerException. pls tell how to get rid of this error.

In above code 'Connect' is the class which i am using for connection purpose. It is working fine as i hav used it with other jsp pages with no probs.connectDb() & processQuery() r method of 'Connect' class.

i hav tried to debug the code, it is clear that str_rid and user_id are able to store the required value...

error comes only after

ResultSet rs = Connect.processQuery(str);

.

note : i hav checked the value of user_rid, while debugging & it does match with one of the row of the 'registration' table. 'fname' is the 2nd field of the table.

So i dont understand why it is throwing 'NullPointerException' even when all variables r declared & initialized..

Please help soon..

[2286 byte] By [richa-jja] at [2007-11-26 23:08:06]
# 1
inside this method: Connect.processQuery(str);there must be a command which caused the NullPointerException.
mimi_tan1985a at 2007-7-10 14:02:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

hi..

u may be right. below is code of the 'processQuery' method

public static ResultSet processQuery(String strSQL)

{

try

{

Statement stmt=con.createStatement();

rs=stmt.executeQuery(strSQL);

return rs;

}

catch(Exception ex)

{

System.out.println("error in processQuery : " + ex);

return null;

}

}

it can throw null, but in my case i dont see any reason for this method to throw 'null'. it is expected to return 'rs' with single row..

can u tell wht could be the other reason ?

richa-jja at 2007-7-10 14:02:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Please replace:

System.out.println("error in processQuery : " + ex);

with:

ex.printStackTrace();

and then see which line caused the NullPointerException.

It could be any reason for the method to throw null exception :

- the connection failed to create statement

- the connection is null itself

All you have to do is to read the stack trace and see which line exactly caused the exception.

mimi_tan1985a at 2007-7-10 14:02:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

hi.. again

i hav just tried with simple query..

String str = "select * from Registration";

then it runs & displays value on page..

looks like problem lies..in this query

String str = "select * from Registration where rid=user_rid";

wht could be it..bocz i hav confirmed there is a matching 'user_rid' in the table ?

richa-jja at 2007-7-10 14:02:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
it should be:String str = "select * from Registration where rid=" + user_rid;
mimi_tan1985a at 2007-7-10 14:02:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
ya...thank u very much.. it works..
richa-jja at 2007-7-10 14:02:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...