Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what could be the reason behind this exception?

 
jyotsana dang
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you provide us more information like, stacktrace
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic