List<NewInfor> list = (List<NewInfor>) session.getAttribute("list"); for (int i=0; i<list.size(); i++){ NewInfor newInfor = list.get(i);什么情况下会取不到里面的值
时间: 2024-03-12 08:42:53 浏览: 23
当 session 中没有名为 "list" 的属性时,`session.getAttribute("list")` 的返回值为 null,此时如果调用 `list.size()` 方法会抛出 NullPointerException,因为 null 没有 size() 方法。因此,为了避免这种情况,我们应该在调用 `list.size()` 之前,先判断一下 list 是否为 null,例如:
```
List<NewInfor> list = (List<NewInfor>) session.getAttribute("list");
if (list != null) {
for (int i=0; i<list.size(); i++){
NewInfor newInfor = list.get(i);
// 处理 newInfor
}
} else {
// 处理 list 为空的情况
}
```
另外,还有一种情况是 list 不为 null,但是其中某个元素为 null,此时调用 `list.get(i)` 会返回 null,如果没有对返回值进行判空处理,可能会导致后续代码出现 NullPointerException。
相关问题
HttpSession session = request.getSession(); List<NewInfor> list; NewsService newsService = new NewsService(); //查询所有新闻 list = newsService.getAllNews(); //存入 session.setAttribute("list", list); //跳转页面 request.getRequestDispatcher("/index.jsp").forward(request,response); List<NewInfor> list = (List<NewInfor>) session.getAttribute("list"); for (int i=0; i<list.size(); i++){ NewInfor newInfor = list.get(i); %> <tr> <td> <a href="getInformation?newsId=<%=newInfor.getId()%>"><%= newInfor.getTitle()%></a> </td> <td><%= newInfor.getAuthor()%></td> <td><%= newInfor.getType()%></td> <td><%= newInfor.getTime()%></td> </tr> <% } %>
这段代码是一个JSP页面,首先它通过调用request对象的getSession()方法获取HttpSession对象,然后创建了一个NewsService对象来查询所有新闻并将结果存入HttpSession中,接着通过调用request对象的getRequestDispatcher()方法跳转到index.jsp页面。
在index.jsp页面中,它通过调用session对象的getAttribute()方法获取之前存入的新闻列表,然后使用循环遍历所有新闻并在页面中以表格的形式展示出来,每一行对应一篇新闻,包含该新闻的标题、作者、类型、时间等信息。当用户点击某篇新闻的标题时,它会跳转到getInformation页面并带上该新闻的ID作为参数。
public String toString() { return "NewInfor{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + ", author='" + author + '\'' + ", type='" + type + '\'' + ", time=" + time + '}'; }List<NewInfor> list = (List<NewInfor>) session.getAttribute("list"); for (int i=0; i<list.size(); i++){ NewInfor newInfor = list.get(i);}
这段代码中,首先定义了一个 `toString()` 方法,它返回一个格式化后的字符串,其中包含了 `NewInfor` 对象的各个属性值。
接下来,在代码的另一部分中,使用 `session.getAttribute("list")` 从会话中获取名为 `list` 的对象,它被强制转换为 `List<NewInfor>` 类型的变量 `list`。然后使用 `for` 循环遍历 `list` 列表中的每个元素,使用 `list.get(i)` 获取第 `i` 个元素,并将它赋值给 `NewInfor` 类型的变量 `newInfor`。
但是,这段代码并没有对 `newInfor` 进行任何操作,只是获取了列表中的元素。如果需要对 `newInfor` 进行操作,可以在循环体中编写适当的代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![sql](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
@RequestMapping("/completed") public String Completed(Model model,HttpSession session) { User user = (User) session.getAttribute("user"); int userid=user.getUserId(); List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4); model.addAttribute(formList); return "order4"; }我这样写传了List到A页面,那么我在A页面中,写标签进行页面跳转,并且要用到List中的数据作为参数也在中传递到下一个控制器,我该怎么写
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)