wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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
Author

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">
<th>
<% out.print("Level"); %>
</th>

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

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

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

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


%>

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

}
if(count==0)
{
{
out.println("<p><b>No records found</b></p>");
}
}
}
catch(Exception e)
{
System.out.println("exception in block"+e);
}
rs.close();
stmt.close();
%>
</table>
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.
Nick.


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
author
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: 614
Hi,
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.
Cheers
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
 
jQuery in Action, 2nd edition
 
subject: what could be the reason behind this exception?
 
Similar Threads
Delete Records from MS Access Database
getObject () not returning values
problem in browsing
how do i delete multiple records
display problem.