java.lang.IllegalArgumentException:迭代变量不能为null

时间:2019-01-31 04:12:52

标签: java spring iterator iteration thymeleaf

enter image description here>坚持了几天。我相信这个问题必须要做

  

与类本身。还是班级之间的关系   问题及其列所属的类。

     

这是我的课程:    Mod

package com.matt.Keyword.models;
    import javax.persistence.*;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;


    @Entity
    @Table(name = "Mods")
    public class Mod {

        @Id
        @GeneratedValue
        private int id;

        @NotNull
        private Integer role;

        @NotNull
        @Size(min = 1, message = "An entry is required")
        private String entry;

        @Column(name = "user_id")
        private Integer userid;

        public Mod(Integer role, String entry) {
            this.role = role;
            this.entry = entry;
        }

        public Mod() {
        }

        public int getId() {

            return id;
        }

        public int getRole() {
            return role;
        }

        public void setRole(Integer role) {
            this.role = role;
        }

        public String getEntry() {
            return entry;
        }

        public void setEntry(String entry) {
            this.entry = entry;
        }

        public Integer getUserid() {
            return userid;
        }

        public void setUserid(Integer userid) {
            this.userid= userid;
        }

    }

帐户

package com.matt.Keyword.models;

    import javax.persistence.*;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;

    @Entity
    public class Account {

        @Id
        @GeneratedValue
        private int id;

        @NotNull
        @Size(min = 3, message = "Try again")
        private String name;

        @NotNull
        @Size(min = 1, message = "A password is required")
        private String password;

        @Column(name = "user_id")
        private Integer userid;

        public Account(String name, String password) {
            this.name = name;
            this.password = password;
        }

        public Account() {
        }

        public int getId() {

            return id;
        }

        public String getName() {

            return name;
        }

        public void setName(String name) {

            this.name = name;
        }

        public String getPassword() {

            return password;
        }

        public void setPassword(String password) {

            this.password = password;
        }

        public Integer getUserid() {
            return userid;
        }

        public void setUserid(Integer userid) {
            this.userid = userid;
        }

    }
  

Account和Mod类非常相似,并且与   用户类中的user_id列。

用户

package com.matt.Keyword.models;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.List;

@Entity
public class User {

    @Id
    @GeneratedValue
    @Column(name = "user_id")
    private int id;

    @Size(min=3, message = "Try again")
    private String email;

    @Size(min=1, message = "A password is required")
    private String password;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    private List<Account> accounts;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    private List<Mod> mods;

    public User(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public User() {}

    public int getId() {

        return id;
    }

    public void setId(Integer id){
        this.id = id;
    }
    public String getEmail() {

        return email;
    }

    public void setEmail(String email) {

        this.email = email;
    }

    public String getPassword() {

        return password;
    }

    public void setPassword(String password) {

        this.password = password;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }


    public List<Mod> getMods() {
        return mods;
    }

    public void setMods(List<Mod> mods) {
        this.mods = mods;
    }
    @Override
    public String toString()
    {
        return email + " " + password;
    }
}
  

当我从mods加载索引页面时,抛出异常   控制器。我正在使用百里香来渲染视图。这是我的mod   控制器。 Mods数据库中已保存了对象,并且在此   调试期间将属性传递给视图的控制器   包含Mod对象的填充列表。     ModController

package com.matt.Keyword.controllers;


    import com.matt.Keyword.models.Mod;
    import com.matt.Keyword.models.User;
    import com.matt.Keyword.models.data.ModDao;
    import com.matt.Keyword.models.data.UserDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.validation.Errors;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    import javax.servlet.http.HttpSession;
    import javax.validation.Valid;

    //test commit note
    //second try
    //third try
    /**
     * Created by LaunchCode
     */
    @Controller
    @RequestMapping("mod")

    public class ModController {

        @Autowired
        private UserDao userDao;

        @Autowired
        private ModDao modDao;

        @RequestMapping(value = "")
        public String index(Model model, HttpSession session) {

            if (session.getAttribute("currentUser") == null) {

                return "redirect:/keyword/login";
            }

            session.getAttribute("currentUser");
            model.addAttribute("title", "Mods");
            model.addAttribute("mods", modDao.findAll());
            return "mods/index";

        }

        @RequestMapping(value = "add", method = RequestMethod.GET)
        public String displayModForm(Model model, HttpSession session) {

            if (session.getAttribute("currentUser") == null) {
                return "redirect:/keyword/login";
            }

            model.addAttribute("title", "Add a new mod");
            model.addAttribute(new Mod());
            return "mods/add";
        }

        @RequestMapping(value = "add", method = RequestMethod.POST)
        public String processAddModForm(@ModelAttribute @Valid Mod newMod,
                                            Errors errors, Model model, HttpSession session) {

            if (errors.hasErrors()) {
                model.addAttribute("title", "List of Mods");
                return "mods/add";
            }
            User currentUser = (User) session.getAttribute("currentUser");

            session.getId();

            currentUser.setId(currentUser.getId());

            newMod.setUserid(currentUser.getId());

            modDao.save(newMod);

            return "redirect:";

        }

    }
  

这是使用百里香叶的mod / index页面。

mod / index

<!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org/">
    <head th:replace="fragments :: head"></head>

    <body class="container">

    <h1 th:text="${title}">Default Title</h1>

    <p th:unless="${mods} and ${mods.size()}">No current mods</p>

    <nav th:replace="fragments :: navigation"></nav>

    <p th:text="${session.currentUser.email}"></p>

    <table class="table">
        <tr>
            <th>Type</th>
            <th>Entry</th>
        </tr>
            <div th:if="${mods} != null">
                <tr th:each="mod : ${mods}">
                    <div th:if="${mod.userid} == ${session.currentUser.id}">
                        <td th:text="${mod.role}"></td>
                        <td th:text="${mod.entry}"></td>
                    </div>
                </tr>
            </div>

    </table>
    </body>
    </html>
  

该异常似乎与for th:each语句相关联。   特别是使用“ mod”迭代器变量。   这是完整错误的堆栈跟踪:   跟踪

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/mods/index.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/mods/index.html]")
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.standard.processor.StandardEachTagProcessor' (template: "mods/index" - line 21, col 17)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 52 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.standard.processor.StandardEachTagProcessor' (template: "mods/index" - line 21, col 17)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)
    at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
    at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
    at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
    at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304)
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278)
    at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186)
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124)
    at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109)
    at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297)
    at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402)
    at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159)
    at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
    ... 54 more
Caused by: java.lang.IllegalArgumentException: Iteration variable cannot be null
    at org.thymeleaf.util.Validate.notNull(Validate.java:37)
    at org.thymeleaf.standard.expression.Each.<init>(Each.java:49)
    at org.thymeleaf.standard.expression.EachUtils.composeEach(EachUtils.java:169)
    at org.thymeleaf.standard.expression.EachUtils.internalParseEach(EachUtils.java:94)
    at org.thymeleaf.standard.expression.EachUtils.parseEach(EachUtils.java:65)
    at org.thymeleaf.standard.processor.StandardEachTagProcessor.doProcess(StandardEachTagProcessor.java:59)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
    ... 67 more

感谢您的帮助。预先感谢您看看 你有时间。

Debugger in mod controller Here is a to link to my repo if you want to download it and give it a run.

Hibernate exception in session

Edit to mod/index controller

1 个答案:

答案 0 :(得分:0)

事实证明,这个问题相当棘手。解决方案的故事如下...

从堆栈跟踪判断:

Caused by: java.lang.IllegalArgumentException: Iteration variable cannot be null

在百里香模板中有一个迭代,其中有一个null变量被迭代。我可以找到的th:each的唯一用途是:

<div th:if="${mods} != null">
    <tr th:each="mod : ${mods}">
         ...
    </tr>
</div>

所以看来modsnull,因此th:each="mod : ${mods}"是问题所在。

但是有<div th:if="${mods} != null">对此进行了检查。还是在那里?以下内容是否会更有用:

<div th:if="${mods != null}">

即。移动},使null检查位于表达式内。

那没有帮助,所以检查了modDao.findAll()的结果,发现了两个元素。所以必须是其他东西。

回顾basics of iteration in Thymeleaf可能表明findAll()返回的集合不是th:each可以迭代的受支持类型之一。

因此,该集合被复制到Iterable中,并使用了不同的名称,问题得以解决。

有趣的是,如果将迭代变量命名为mod,问题仍然会发生。原来mod是hyleleaf中的关键字,用于模块操作(%)。

最终,看似正确的代码有两个问题:

  1. DAO返回了一个集合,但是没有一个th:each可以对其进行迭代的集合。
  2. mod是Thymeleaf中的关键字,不应用作变量名。