title: NullPointerException when iterating over ArrayList in jsp
body: <p>I am displaying a list which contains objects which store message information. The list is loaded through getting the request as such:</p>
  
  <pre><code>ArrayList&lt;MessageModel&gt; list = (ArrayList&lt;MessageModel&gt;) request.getAttribute("msgList");
  </code></pre>
  
  <p>I am trying to display information contained in each object as such:</p>
  
  <pre><code>for(MessageModel msg : list){
                  String messageTo = msg.toAddress;
                  String messageSub = msg.messageSubject;
                  out.println(messageTo+ " " +messageSub);
              }
  </code></pre>
  
  <p>I am getting a  NullPointerException at the for each statement.</p>
  
  <p>As an aside, I am using this approach to debug as I want to check whether or not I can display the content of the MessageModel object at all. I tried using JSTL forEach tag but I could not access the object attributes.</p>
  
  <pre><code> &lt;c:forEach items="${list}" var="current"&gt;
       &lt;li&gt;&lt;c:out value="${current.toAddress}"/&gt;&lt;/li&gt;
   &lt;/c:forEach&gt;
  </code></pre>
  
  <p>I set the attribute like this :</p>
  
  <pre><code> ArrayList&lt;MessageModel&gt; list =(ArrayList&lt;MessageModel&gt;) request.getAttribute("msgList");
   pageContext.setAttribute("messages", list);
  </code></pre>
  
  <p>This results in the following error message when run : </p>
  
  <pre><code>org.apache.jasper.JasperException: /inbox.jsp (line: 37, column: 12) According to TLD or attribute directive in tag file, attribute items does not accept any expressions
  </code></pre>
  
  <p>And intelliJ says that it cannot resolve the variable list.</p>
  
  <p>EDIT :</p>
  
  <p>I forward the request from a servlet that handles user login as follows :</p>
  
  <pre><code> ArrayList&lt;MessageModel&gt; msglist = MessageModel.getRecievedMessages(username);
   request.setAttribute("msgList", msglist);
  </code></pre>
  
  <p>I am trying to pass the ArrayList that contains the message objects and then iterate over the list and display it in the jsp. </p>
  
  <p>Is there a possibility for the jsp itself to get the information ? I have a separate java class which handles the messages with a bunch of helper functions. So can I call the method that retrieves all those messages and display them without explicitly setting a request attribute in the servlet ? Because I will be navigating between other pages in the application.</p>
  
  <p>Sorry if my vocabulary was off, this is the first time I am working with jsp's and servlets. </p>