Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer..

 
jyotsana dang
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post the exception trace?
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!!
My bad, I mean using something like DAO pattern and create some kind of abstraction.
Damn it.
Dan.
 
jyotsana dang
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotit working !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic