<%
List listSonBoard =(List)mapboard.get(new Integer(mainBoard.getBoardId()));
for(int j=0;j<listSonBoard.size();j++) //java.lang.NullPointerException
{
Board sonBoard=(Board)listSonBoard.get(j);
Topic topic =new Topic();
User user=new User();
int boardId =sonBoard.getBoardId();
List listTopic=new TopicDAO().findListTopic(1, boardId);
if(listTopic!=null&&listTopic.size()>0)
{
//取得最后发表的帖子
topic=(Topic)listTopic.get(0);
user=new UserDAO().findById(topic.getuId());
}
%>
上面 for(int j=0;j<listSonBoard.size();j++) 出现 //java.lang.NullPointerException
加了判断
if(listSonBoard != null){
for(.....){ ..... }
}
有部分可以在 mainBoard.getBoardId()<=3 时显示的 ,不可以显示了.
List listSonBoard =(List)mapboard.get(new Integer(mainBoard.getBoardId()));
if(listSonBoard!=null){//这句话加上肯定是对的,对,“mainBoard.getBoardId()<=3 时显示的 ,不可以显示了.”肯定没影响,看看是不是其他原因导致不显示。
for(int j=0;j<listSonBoard.size();j++) //java.lang.NullPointerException
{
Board sonBoard=(Board)listSonBoard.get(j);
Topic topic =new Topic();
User user=new User();
int boardId =sonBoard.getBoardId();
List listTopic=new TopicDAO().findListTopic(1, boardId);
if(listTopic!=null&&listTopic.size()>0)
{
//取得最后发表的帖子
topic=(Topic)listTopic.get(0);
user=new UserDAO().findById(topic.getuId());
}
}
打印出来看看listSonBoard这里面的东西
public Map findBoard(){
//查询板块的SQL语句
String sql="select * from board order by parentId,boardId";
try {
connection=this.getConn();//得到数据库连接
pstmt=connection.prepareStatement(sql);//得带PreparedStatement对象
rs=pstmt.executeQuery();//执行SQL得到结果集
sonList=new ArrayList(); //实例化
//循环将版块信息封装成Map
while(rs.next())
{
if(parentId!=rs.getInt("parentId"))
{
//将上一组子版块保存到Map中
map.put(new Integer(parentId), sonList);
//重新产生一个ArrayList对象,用于存放下一组子版块
sonList=new ArrayList();
//为parentNo重新设值,用于Map的新的Key值
parentId=rs.getInt("parentId");
}
//板块对象
Board board=new Board();
//板块ID
board.setBoardId(rs.getInt("boardId"));
//版块名称
board.setBoardName(rs.getString("boardName"));
//保存数以同一个父版块的子版块
sonList.add(board);
}
//保存最后一个sonList
map.put(new Integer(parentId), sonList);
} catch (Exception e) {
e.printStackTrace();
}
finally
{
closeAll(connection, pstmt, rs);
}
return map;
}
0) { //取得最后发表的帖子 topic=(Topic)listTopic.get(0); user=new UserDAO().findById(topic.getuId()); } %>论坛 | 主题 | 最后发表 | |
![]() | [] |
---|
论坛 | 主题 | 最后发表 | |
![]() | [] |
---|
<tr>
<td colspan="2" align="left" valign="middle">
<span class="STYLE4"> 论坛</span>
</td>
<td width="5%" class="STYLE4">主题</td>
<td width="45%" class="STYLE4">最后发表</td>
</tr>
<%
List listMainBoard =(List) mapboard.get(new Integer(0));
Board mainBoard=new Board();
for(int i=0;i<listMainBoard.size();i++)
{
mainBoard=((Board)listMainBoard.get(i));
%>
<tr>
<td colspan="4"><%=mainBoard.getBoardName() %></td>
</tr>
<%
List listSonBoard =(List)mapboard.get(new Integer(mainBoard.getBoardId()));
if(listSonBoard!=null){
for(int j=0;j<listSonBoard.size();j++)
{
Board sonBoard=(Board)listSonBoard.get(j);
Topic topic =new Topic();
User user=new User();
int boardId =sonBoard.getBoardId();
List listTopic=new TopicDAO().findListTopic(1, boardId);
if(listTopic!=null&&listTopic.size()>0)
{
//去的最后发表的帖子
topic=(Topic)listTopic.get(0);
user=new UserDAO().findById(topic.getuId());
}
%>
<tr>
<td width="5%"> </td>
<th align="left"><img width="20" height="20" src="<%=request.getContextPath()%>/Resource/post.png"/>
<a href="./list.jsp"><%=sonBoard.getBoardName() %></a>
</th>
<td align="center"><%=new TopicDAO().FindCountTopic(boardId) %></td>
<th>
<span>
<a href="detail.jsp"><%=topic.getTitle() %></a>
</span>
<br/>
<span><%=user.getuName()%></span>
<span>[<%=topic.getPublishTime() %>]</span>
</th>
</tr>
<%
}
}
}
%>
麻烦大家看下,谢谢各位了
key= 0 and value= [cn.cslg.bbs.bean.Board@11e1a67, cn.cslg.bbs.bean.Board@109564e, cn.cslg.bbs.bean.Board@8a6205, cn.cslg.bbs.bean.Board@f5c150]
key= 1 and value= [cn.cslg.bbs.bean.Board@1993e8b, cn.cslg.bbs.bean.Board@b3a750, cn.cslg.bbs.bean.Board@1666189, cn.cslg.bbs.bean.Board@1b3c0f4]
key= 2 and value= [cn.cslg.bbs.bean.Board@132d12f, cn.cslg.bbs.bean.Board@9278fe, cn.cslg.bbs.bean.Board@3039cc, cn.cslg.bbs.bean.Board@1cbf6de]
key= 3 and value= [cn.cslg.bbs.bean.Board@dbf621, cn.cslg.bbs.bean.Board@1092c4c]
key= 14 and value= [cn.cslg.bbs.bean.Board@aa9c38]