This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JSP and the fly likes what could be the reason behind this exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "what could be the reason behind this exception?" Watch "what could be the reason behind this exception?" New topic

what could be the reason behind this exception?

jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
here is my jsp code for browsing the records that have been inserted:
Connection con=null;
java.sql.Statement stmt=null;
ResultSet rs=null;
boolean y=false;
int i;
String xy=(String)session.getAttribute("y");

<table border="1" cellpadding="1" cellspacing="1">
<% out.print("Level"); %>

<% out.print("Project Title"); %>

<% out.print("Student Names");%>

<% out.print("Co-Supervisor"); %>

<% out.print("Year"); %>
con=DriverManager.getConnection("jdbc dbc:mech_iit");
int count=0;
rs=stmt.executeQuery("select top 4* from projects_supervised where user_login='"+xy+"' order by project_id");
while( {


<td><% out.print(rs.getString(2)); %>
<td><% out.print(rs.getObject(3).toString()); %>
<td><% out.print(rs.getObject(4).toString()); %>
<td><% out.print(rs.getObject(5).toString()); %>
<% out.print(rs.getObject(6).toString()); %>

out.println("<p><b>No records found</b></p>");
catch(Exception e)
System.out.println("exception in block"+e);
This jsp page throws a null pointer exception. But in fact when i use the same code for seeing other inserted records from a different table then the page works fine.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Can you provide us more information like, stacktrace

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

Joined: Nov 07, 2003
Posts: 4982
The only case I think of the NullPointerException is that, when you execute the statement, exception thrown. Since you catch it, and thus the code continues to execute. But then, as the code: stmt.executeQuery(...) throws exception, rs may still be null.
Then, when you call rs.close(), this causes the NullPointerException.
But of course, we need to trace the error to confirm the problem.

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
David Peterson
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
Or perhaps more likely, since you say it works OK on a different table, one of the records in the "projects_supervised" table contains a NULL value for one of the fields (e.g. project title), and so rs.getObject() returns null. You can't call toString() on a null.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
I guess you can try without toString()...if it prints address insted of value then try out.println(rs.getObject()+"");
and wondering why you are not using rs.getString() instead of getObject.

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
I agree. Here's the link:
subject: what could be the reason behind this exception?
It's not a secret anymore!