其他分享
首页 > 其他分享> > 遇到一个大坑

遇到一个大坑

作者:互联网

在测试mapper插数据时,
discussPostMapper.insertDiscussPost(new DiscussPost(0,100, “leslie”, “哈喽”, 1, 1, new Date(), 100, 55));
插入的userId为100,但是在user表中没有id为100的用户,
调用下面的业务方法,User user = userService.findUserById(post.getUserId()),查出user为null,然后在前端页面th:src="${map.user.headerUrl}",产生了null.headUrl报错。最难受的是知道这里出了问题,但是由于出入的数据在最后面,debug调试是前面都是正常的没问题,但是数据太多没有调试到最后面。。。
所以啊,以后遇到null异常也要要想到,数据库的数据是否合法呀

	@RequestMapping(value = {"/index","/"})
	public String getIndexPage(Model model, Page page) {
		// 方法调用栈,SpringMVC会自动实例化Model和Page,并将Page注入Model
		// 所以,在thymeleaf中可以直接访问Page对象中的数据
		//总数
		page.setRows(discussService.selectDiscussPostRows(0));
		//查询的路径
		page.setPath("/index");
		List<DiscussPost> list = discussService.selectDiscussPosts(0, page.getOffset(), page.getLimit());
		List<Map<String, Object>> discussPosts = new ArrayList<>();
		if (list != null) {
			for (DiscussPost post : list) {
				Map<String, Object> map = new HashMap<>();
				map.put("post", post);
				User user = userService.findUserById(post.getUserId());
				map.put("user", user);
				discussPosts.add(map);
			}
		}
		model.addAttribute("discussPosts", discussPosts);
		return "index";
	}
<ul class="list-unstyled">
                <li class="media pb-3 pt-3 mb-3 border-bottom" th:each="map:${discussPosts}">
                    <a href="site/profile.html">
                        <img th:src="${map.user.headerUrl}" class="mr-4 rounded-circle" alt="用户头像"
                             style="width:50px;height:50px;">
                    </a>
                    <div class="media-body">
                        <h6 class="mt-0 mb-3">
                            <a href="#" th:utext="${map.post.title}">备战春招,面试刷题跟他复习,一个月全搞定!</a>
                            <span class="badge badge-secondary bg-primary" th:if="${map.post.type==1}">置顶</span>
                            <span class="badge badge-secondary bg-danger" th:if="${map.post.status==1}">精华</span>
                        </h6>
                        <div class="text-muted font-size-12">
                            <u class="mr-3" th:utext="${map.user.username}">寒江雪</u> 发布于 <b
                                th:text="${#dates.format(map.post.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15
                            15:32:18</b>
                            <ul class="d-inline float-right">
                                <li class="d-inline ml-2">赞 11</li>
                                <li class="d-inline ml-2">|</li>
                                <li class="d-inline ml-2">回帖 7</li>
                            </ul>
                        </div>
                    </div>
                </li>
            </ul>

下面是报的错误:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "map.user.headerUrl" (template: "index" - line 132, col 30)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'headerUrl' cannot be found on null
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:92)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:337)
	at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
	... 64 common frames omitted

标签:PropertyOrFieldReference,java,大坑,遇到,一个,springframework,spel,org,expression
来源: https://blog.csdn.net/weixin_44447330/article/details/122291201