• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

null pointer..

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please post the exception trace?
 
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gotit working !!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic