title: Springframework - property or field cannot be found on null
body: <p>I am working on a project and keep getting an error. </p>
  
  <p><code>&lt;span style="font-style: italic;" th:text="${user != null} ? ${#strings.toUpperCase(user.username)} : 'user is null'"&gt;&lt;/span&gt;</code></p>
  
  <pre><code>EL1007E: Property or field 'id' cannot be found on null
  </code></pre>
  
  <p>I'm new to thyemeleaf and spring so still learning so not sure what you would need to help figure out the issue so I will update when I know what I need to provide.  </p>
  
  <p>Header HTML where login/New user modal is</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;body&gt;
    &lt;div th:fragment="navbar"&gt;
      &lt;div class="container"&gt;
        &lt;nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top"&gt;
          &lt;a class="navbar-brand active" href="https://inplaydesign.com/index.html"&gt;d|b&lt;/a&gt;
          &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"&gt;
       &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
      &lt;/button&gt;
          &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt;
            &lt;ul class="navbar-nav mr-auto"&gt;
              &lt;li class="nav-item"&gt;
                &lt;a class="nav-link" href="https://blog.inplaydesign.com"&gt;blog&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="nav-item"&gt;
                &lt;a class="nav-link" href="/gallery/gallery.html"&gt;gallery&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="nav-item"&gt;
                &lt;a class="nav-link" href="#"&gt;shop&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="nav-item"&gt;
                &lt;a class="nav-link" href="/faq/faq.html"&gt;f.a.q.&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="nav-item"&gt;
                &lt;a class="nav-link" href="/contact/contact.html"&gt;contact&lt;/a&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
            &lt;button type="button" class="btn btn-outline-dark"&gt;
        &lt;i class="fa fa-shopping-cart"&gt;&lt;/i&gt; Shopping Cart
       &lt;/button&gt;
            &lt;button sec:authorize="isAnonymous()" type="button" class="btn btn-outline-dark" data-toggle="modal" data-target="#accountAccess"&gt;
        &lt;i class="fa fa-id-card"&gt;&lt;/i&gt; Account
       &lt;/button&gt;
            &lt;a sec:authorize="isAuthenticated()" th:href="@{/myProfile}"&gt;
              &lt;button type="button" class="btn btn-outline-dark"&gt;
         &lt;i class="fa fa-id-card"&gt;&lt;/i&gt; Account
        &lt;/button&gt;
            &lt;/a&gt;
            &lt;a sec:authorize="isAuthenticated()" th:href="@{/logout}"&gt;
              &lt;button type="button" class="btn btn-outline-dark"&gt;
         &lt;i class="fa fa-power-off"&gt;&lt;/i&gt; log-out
        &lt;/button&gt;
            &lt;/a&gt;
          &lt;/div&gt;
        &lt;/nav&gt;
      &lt;/div&gt;
      &lt;!-- Account modal --&gt;
      &lt;div class="modal fade" id="accountAccess" tabindex="-1" role="dialog" aria-hidden="true"&gt;
        &lt;div class="modal-dialog" role="document"&gt;
          &lt;div class="modal-content"&gt;
            &lt;div class="modal-header"&gt;
              &lt;h5 class="modal-title"&gt;Account Login and Creation
              &lt;/h5&gt;
            &lt;/div&gt;
            &lt;div class="modal-body"&gt;
              &lt;ul class="nav nav-tabs" role="tablist"&gt;
                &lt;li class="nav-item" th:classappend="${classActiveNewAccount}? 'active'"&gt;
                  &lt;a class="nav-link" data-toggle="tab" href="#tab-1" role="tab"&gt;Sign up&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class="nav-item" th:classappend="${classActiveLogin}? 'active'"&gt;
                  &lt;a class="nav-link active" data-toggle="tab" href="#tab-2" role="tab"&gt;User Login&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class="nav-item" th:classappend="${classActiveForgetPassword}? 'active'"&gt;
                  &lt;a class="nav-link" data-toggle="tab" href="#tab-3" role="tab"&gt;Forgot Password&lt;/a&gt;
                &lt;/li&gt;
              &lt;/ul&gt;
              &lt;div class="tab-content" style="margin-top: 10px;"&gt;
                &lt;!-- New User --&gt;
                &lt;div class="tab-pane fade" id="tab-1" role="tabpanel" th:classappend="${classActiveNewAccount}? 'in active'"&gt;
                  &lt;form th:action="@{/newUser}" method="post"&gt;
                    &lt;div class="container"&gt;
                      &lt;div class="form-group"&gt;
                        &lt;label for="email"&gt;Email Address&lt;/label&gt;&amp;nbsp;
                        &lt;div class="alert alert-danger" th:if="${emailExists}"&gt;"Email already exists"&lt;/div&gt;
                        &lt;input type="email" class="form-control" id="email" name="email" placeholder="Enter Email Address" /&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                        &lt;label for="NewUsername"&gt;Username&lt;/label&gt;&amp;nbsp;
                        &lt;div class="alert alert-danger" th:if="${usernameExists}"&gt;"Username already exists"&lt;/div&gt;
                        &lt;input type="text" class="form-control" id="newUsername" name="NewUsername" tabindex="0" placeholder="Enter Username" /&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="modal-footer"&gt;
                      &lt;button type="reset" class="btn btn-outline-dark btn-sm"&gt;
             &lt;i class="fa fa-refresh"&gt;&lt;/i&gt; Reset
            &lt;/button&gt;
                      &lt;button type="button" class="btn btn-outline-dark btn-sm"&gt;
             &lt;i class="fa fa-user-plus"&gt;&lt;/i&gt; Create New Account
            &lt;/button&gt;
                    &lt;/div&gt;
                  &lt;/form&gt;
                &lt;/div&gt;
  
                &lt;!-- login --&gt;
                &lt;div class="tab-pane fade show active" id="tab-2" role="tabpanel" th:classappend="${classActiveLogin}? 'active'"&gt;
                  &lt;div class="alert alert-danger" th:if="${param.error != null}"&gt;
                    &lt;small&gt;Please check your credentials and try again.&lt;/small&gt;
                  &lt;/div&gt;
                  &lt;form th:action="@{/login}" method="post"&gt;
                    &lt;div class="container"&gt;
                      &lt;div class="form-group"&gt;
                        &lt;label for="username"&gt;Username&lt;/label&gt;
                        &lt;input type="text" class="form-control" id="username" name="username" placeholder="Enter Username" /&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                        &lt;label for="password"&gt;Password&lt;/label&gt;
                        &lt;input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" /&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="modal-footer"&gt;
                      &lt;button type="submit" class="btn btn-outline-dark btn-sm"&gt;
             &lt;i class="fa fa-sign-in"&gt;&lt;/i&gt; Log-In
            &lt;/button&gt;
                    &lt;/div&gt;
                  &lt;/form&gt;
                &lt;/div&gt;
  
                &lt;!-- Forgot password --&gt;
                &lt;div class="tab-pane fade" id="tab-3" role="tabpanel" th:classappend="${classActiveForgetPassword}? 'in active'"&gt;
                  &lt;div th:if="${emailNotExist}" class="alert alert-danger"&gt;Email does not exist&lt;/div&gt;
                  &lt;div th:if="${forgetPasswordEmailSent}" class="alert alert-success"&gt;Please check your email for password reset token.&lt;/div&gt;
                  &lt;form action=""&gt;
                    &lt;div class="container"&gt;
                      &lt;div class="form-group"&gt;
                        &lt;label for="recoverEmail"&gt;User Email&lt;/label&gt;
                        &lt;input type="email" class="form-control" id="recoverEmail" placeholder="Enter Recovery Email" name="email" /&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="modal-footer"&gt;
                      &lt;button type="submit" class="btn btn-outline-dark btn-sm"&gt;
             &lt;i class="fa fa-external-link-square"&gt;&lt;/i&gt; Submit
            &lt;/button&gt;
                    &lt;/div&gt;
                  &lt;/form&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div th:fragment="body-bottom-scripts"&gt;
      &lt;!-- Optional JavaScript --&gt;
      &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;
      &lt;script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="/js/bootstrap.min.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;/div&gt;
  &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import java.util.HashSet;
  import java.util.Locale;
  import java.util.Set;
  import java.util.UUID;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.mail.SimpleMailMessage;
  import org.springframework.mail.javamail.JavaMailSender;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  
  import com.digitalblasphemy.domain.User;
  import com.digitalblasphemy.domain.security.PasswordResetToken;
  import com.digitalblasphemy.domain.security.Role;
  import com.digitalblasphemy.domain.security.UserRole;
  import com.digitalblasphemy.service.UserService;
  import com.digitalblasphemy.service.impl.UserSecurityService;
  import com.digitalblasphemy.utility.MailConstructor;
  import com.digitalblasphemy.utility.SecurityUtility;
  
  @Controller
  public class HomeController {
  
    @Autowired
    private JavaMailSender mailSender;
  
    @Autowired
    private MailConstructor mailConstructor;
  
    @Autowired
    private UserService userService;
  
    @Autowired
    private UserSecurityService userSecurityService;
  
    @RequestMapping("/")
    public String index() {
      return "index";
    }
  
    @RequestMapping("/myAccount")
    public String myAccount() {
      return "myAccount";
    }
  
    @RequestMapping("/login")
    public String login(Model model) {
      model.addAttribute("classActiveLogin", true);
      return "myProfile";
    }
  
    @RequestMapping("/forgetPassword")
    public String forgetPassword(
      HttpServletRequest request,
      @ModelAttribute("email") String email,
      Model model
    ) {
      model.addAttribute("classActiveForgetPassword", true);
      User user = userService.findByEmail(email);
  
      if (user == null) {
        model.addAttribute("emailNotExist", true);
        return "index";
      }
  
      String password = SecurityUtility.randomPassword();
  
      String encryptedPassword = SecurityUtility.passwordEncoder().encode(password);
      user.setPassword(encryptedPassword);
  
      userService.save(user);
  
      String token = UUID.randomUUID().toString();
      userService.createPasswordResetTokenForUser(user, token);
  
      String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
  
      SimpleMailMessage newEmail = mailConstructor.constructResetTokenEmail(appUrl, request.getLocale(), token, user, password);
  
      mailSender.send(newEmail);
  
      model.addAttribute("forgetPasswordEmailSent", "true");
  
      return "index";
    }
  
    @RequestMapping(value = "/newUser", method = RequestMethod.POST)
    public String newUserPost(
      HttpServletRequest request,
      @ModelAttribute("email") String userEmail,
      @ModelAttribute("username") String username,
      Model model
    ) throws Exception {
      model.addAttribute("classActiveNewAccount", true);
      model.addAttribute("email", userEmail);
      model.addAttribute("username", username);
  
      if (userService.findByUsername(username) != null) {
        model.addAttribute("usernameExists", true);
  
        return "index";
      }
  
      if (userService.findByEmail(userEmail) != null) {
        model.addAttribute("emailExists", true);
  
        return "index";
      }
  
      User user = new User();
      user.setUsername(username);;
      user.setEmail(userEmail);
  
      String password = SecurityUtility.randomPassword();
  
      String encryptedPassword = SecurityUtility.passwordEncoder().encode(password);
      user.setPassword(encryptedPassword);
  
      Role role = new Role();
      role.setRoleId(1);
      role.setName("ROLE_USER");
      Set &lt; UserRole &gt; userRoles = new HashSet &lt; &gt; ();
      userRoles.add(new UserRole(user, role));
      userService.createUser(user, userRoles);
  
      String token = UUID.randomUUID().toString();
      userService.createPasswordResetTokenForUser(user, token);
  
      String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
  
      SimpleMailMessage email = mailConstructor.constructResetTokenEmail(appUrl, request.getLocale(), token, user, password);
  
      mailSender.send(email);
  
      model.addAttribute("emailSent", "true");
  
      return "myAccount";
    }
  
    @RequestMapping("/newUser")
    public String newUser(
      Locale locale,
      @RequestParam("token") String token,
      Model model) {
  
      PasswordResetToken passToken = userService.getPasswordResetToken(token);
  
      if (passToken == null) {
        String message = "Invalid Token.";
        model.addAttribute("message", message);
        return "redirect:/badRequest";
      }
  
      User user = passToken.getUser();
      String username = user.getUsername();
  
      UserDetails userDetails = userSecurityService.loadUserByUsername(username);
  
      Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
  
      SecurityContextHolder.getContext().setAuthentication(authentication);
  
      model.addAttribute("user", user);
  
      model.addAttribute("classActiveEdit", true);
      return "myProfile"; //possibly change to index page
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have this in my Application.java file to create a user for testing purposes.  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>@Override
  public void run(String...args) throws Exception {
    User user1 = new User();
    user1.setFirstName("Kyle");
    user1.setLastName("Drew");
    user1.setUsername("kd");
    user1.setPassword(SecurityUtility.passwordEncoder().encode("p"));
    user1.setEmail("kd@me.com");
    Set &lt; UserRole &gt; userRoles = new HashSet &lt; &gt; ();
    Role role1 = new Role();
    role1.setRoleId(1);
    role1.setName("ROLE_USER");
    userRoles.add(new UserRole(user1, role1));
  
    userService.createUser(user1, userRoles);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is a picture of the variables after debugging. </p>
  
  <p><a href="https://i.stack.imgur.com/QbdEB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QbdEB.jpg" alt="variables"></a></p>