Home > Software design >  Method call: Attempted to call method on null context object
Method call: Attempted to call method on null context object

Time:01-19

When i load my app page, i give next Exception

 org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "instructor.user.getUserEmail()" (template: "views/instructorCreate" - line 84, col 6)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getUserEmail() on null context object
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "instructor.user.getUserEmail()" (template: "views/instructorCreate" - line 84, col 6)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getUserEmail() on null context object

My version Thymeleaf 3.1 and Spring 6.0.2

My Html Thymeleaf page

<div >
                <label for="userEmail" > Email :</label>
                <input id="userEmail" type="email" name="userEmail" 
                    th:value="${instructor.user.getUserEmail()}" required>
                    <span th:errors="${instructor.user.email}"> </span>
            </div>
            
            <div >
                <label for="userPassword" > Password :</label>
                <input id="userPassword" type="text" name="userPassword" 
                    th:value="${instructor.user.getUserPassword()}" required>
            </div>

User Getter

public String getUserEmail() {
        return userEmail;
    }

Instructors user field;

@OneToOne (cascade = CascadeType.REMOVE)
    @JoinColumn (name = "user_id", referencedColumnName = "user_id", nullable = false)
    private User user;

If you know where my problem, i will be thanks to you

CodePudding user response:

Numerous problems here, so please follow the pattern in the Thymeleaf documentation.

You can use the pattern of either

<input type="text" th:field="*{propertyNameHere}" />

Or

<input type="text" th:name="{propertyNameHere}" />

So for your example, the error message is complaining about instructor.user.getUserEmail(). If you are referring to this property, it would be instructor.user.userEmail.

But in all likelihood, you can remove that altogether with something like:

<input type="text" th:name="{email}"  placeholder="Email">

or even potentially

<input name="email" type="text"  placeholder="Email"/>

And revisit the design for your Instructor class. Why would an Instructor have a User property? Would it make sense to be a subclass or interface? Also, no need to name it userEmail when email would suffice.

  • Related