Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Null Pointer Exception in JSP

843840Oct 23 2008 — edited Oct 25 2008
Hi there. I'm fairly new to Java and I hope this is the correct place to post this. :)

I'm trying to build a partial Bulletin Board using a servlet, two java beans, and JSP. One of the JSP pages lists all messages from an associated MS Access database.

Am constantly getting a Null Pointer Exception when I try to view this page.

Please help! I have no idea where I'm going wrong, so all advice is appreciated.

I've posted the section of the servlet this pertains to, as well as the JSP, below.
//-------------------------------------------------------- 
  public void viewMessageList(HttpServletRequest request,
                      HttpServletResponse response )
                      throws IOException, ServletException
  {
    // Set up SQL...
    String query = "SELECT * from message";
    
    ResultSet rs = JDBCQuery( query );
    if( rs == null )
    {
      showError( request, response, errorMessage );
      return;
    }

    // Copy all records into objects and stick
    // the objects into a Collection...
    
    List messageList = null;
    messageList = new ArrayList();
    try
    {
      while( rs.next() )
      {
        MessageBean bean = new MessageBean();
        bean.setMessageID( rs.getString( "messageID" ) );
        bean.setUserID( rs.getString( "userID" ) );
        bean.setSubject( rs.getString( "subject" ) );
        bean.setMessage( rs.getString( "message" ) );
        bean.setDateposted( rs.getString( "dateposted" ) );
        messageList.add( bean );
      }
      rs.close();
    }
    catch( SQLException sqlx )
    {
      showError(  request, response, "Cannot retrieve records" );
      return;
    }
    
    // Place the collection into menory...
    HttpSession session = request.getSession( true );
    request.setAttribute( "messagelist", messageList );
    
    // Delegate to JSP for the presentation...
    RequestDispatcher rd = context.getRequestDispatcher( "/ViewMessages2.jsp" );
    rd.forward( request, response );
    
  }
JSP
<html>

<head>
		
	<title>Online Communication Forum - View Messages</title>
	
	<link rel="stylesheet" href="OnlineForum.css" />
	
</head>
	
<body>

<%@page import="java.util.*"%>
<%@page import="java.sql.*" %>
<%@page import="OnlineForum.TinOfBeans.MessageBean"%>


<div class="outer">
	<div class="banner"><h1>Online Communication Forum</h1>
	<p /></div> <!-- end banner -->
		<div class="header"><h2 align="left"><a href="ViewMessages2.jsp">View Messages</a>  |  <a href="PostMessage.jsp">Post Message</a>  |  <a href="Logout.jsp">Logout</a></h2></div> <!-- end header -->
		<div class="inner">
	

		
		<table>
			<%
				List list = (List)session.getAttribute("messagelist");
				
				if( list.size() == 0 )
				{
				%>
				<tr><td>Sorry, no messages have been returned.</td></tr>
				<%
				return;
								
				}
				
				
				for( int i=0; i < list.size(); i++)
				{
					MessageBean bean = (MessageBean)list.get(i);

				%>
				<TD align=center><dd><A HREF="servlet/MessageController?action=viewmessages&messageID=<%=bean.getMessageID()%>"><%=bean.getMessageID()%></A></td>
				<td><A HREF="servlet/MessageController?action=viewmessages&messageID=<%=bean.getMessageID()%>"><%=bean.getUserID()%></a></td>
				<td><A HREF="servlet/MessageController?action=viewmessages&messageID=<%=bean.getMessageID()%>"><%=bean.getSubject()%></a></td>
				<td><A HREF="servlet/MessageController?action=viewmessages&messageID=<%=bean.getMessageID()%>"><%=bean.getDateposted()%></a></td>
			</tr>
			<%
				}
			%>
			
		</table>
		
		
	</div><!-- end inner -->
	</div><!-- end outer -->
</BODY>
</HTML>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2008
Added on Oct 23 2008
23 comments
900 views