aspose file tools*
The moose likes JSP and the fly likes null pointer.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "null pointer.." Watch "null pointer.." New topic
Author

null pointer..

jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
wrote this code.. for executing the search..
<%!
Connection con=null;
java.sql.Statement stmt=null;
ResultSet rs=null;
%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:mech_iit");
stmt = con.createStatement();
String fname=request.getParameter("fname").toUpperCase();
String lname=request.getParameter("lname").toUpperCase();
String mainquery="";
String subquery="";

if(!fname.trim().equals(""))
subquery="select upper(fname) from login where fname like '%"+fname+"%'";

System.out.println("select upper(fname) from login where fname like '%"+fname+"%'");
if(!lname.trim().equals(""))
{

if(subquery.length()>10)
{
subquery=subquery+"and";
}
subquery=subquery+"select uppper(lname) from login where lname like '%"+lname+"%'";
System.out.println(subquery+"select uppper(lname) from login where lname like '%"+lname+"%'");
}
mainquery="select fname,lname from login where "+subquery+"";


while(rs.next())
{
fname = rs.getString("fname");
lname = rs.getString("lname");
%>
<table border="1" width="400">
<tr><td><%=fname %></td>
<td><%=lname %></td>
</tr>
<%
}


}
catch(Exception e)
{
System.out.println(e);
}
%>
</table>
getting a null pointer exception
why so?? can anyone explain..
thanks
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Could you please post the exception trace?


Mani
Quaerendo Invenietis
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Hi,
this is why you are getting null pointer exception
rs = null;
while(rs.next())

between these two lines you need to initialize resultset i.e. rs

may be something like this:
rs = stmt.executeQuery(mainquery);


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Also it is possible that this is very inefficient in the long run.
Its better to move your scriplet code to a differnt include file(so that it can be easily maintained), and that will improve the readability of the JSP.
Other suggestion is to move the database processing to the server side and just pass the result set in a well defined collection format after processing it on the server side.
Dan.


Kishore
SCJP, blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Other suggestion is to move the database processing to the server side

Huh? All Java code in a JSP or servlet is server-side code.
If you mean that it'd be best to abstract the code that performs the database access into a Model-component object, then that would make sense.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
oops!!
My bad, I mean using something like DAO pattern and create some kind of abstraction.
Damn it.
Dan.
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
thanks a tonne everyone for ur suggestions and special thanks to praful for pointing out that error.
now iam geting an sql exception in my select statement..wonder why?? will try to solve that..
anyways..thanks ..
select fname from login where fname like '%s%'
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect
syntax near the keyword 'select'.
[ February 23, 2004: Message edited by: jyotsana dang ]
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
gotit working !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null pointer..