Home > Enterprise >  Property or field 'userName' cannot be found on null
Property or field 'userName' cannot be found on null

Time:10-18

here is the code:- form.html :

<div >
    <div >
        <div >
    
        
            <form th:action="@{/process}" method="post" th:object="${loginData}">
            
                <div >
                    <label for="exampleInputEmail1" >Email address</label> 
                    <input 
                        type="text"
                        name="userName" 
                        
                        
                        th:value="${loginData.userName}"
                        
                        id="exampleInputEmail1" 
                        aria-describedby="emailHelp">
                        
                </div>
                
                
                <div >
                    <label for="exampleInputPassword1" >Password</label>
                    
                    <input 
                        type="email"
                        name="email" 
                        
                        th:value="${loginData}"
                        
                        
                        id="exampleInputPassword1">
                </div>
                
                
                <div >
                    <input 
                        type="checkbox" 
                         
                        id="exampleCheck1">
                    
                    <label  for="exampleCheck1">Check me out</label>
                </div>
                
                <button type="submit" >Submit</button>
                
            </form>
            
        </div>
    </div>
</div>

MyController :

@GetMapping("/form") public String formHandler(Model m ) {

            System.out.println("opening form");
            LoginData loginData = new LoginData();
            if(loginData != null ) {
                m.addAttribute("login", new LoginData());
                System.out.println("YAY");
            }else {
                System.out.println("Bhag Bh*****ke");
            }
        
            return "form1";
        }
        
 //handler for process form
 @PostMapping("/process")
 public String processform(@ModelAttribute("loginData") LoginData loginData) {
            
    System.out.println(loginData);
    return"success";
}

success.html :

  <!doctype html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
      <meta charset="UTF-8" />
      <title>Success</title>
    </head>
    <body>
          <h1>Welcome to this success page</h1>
          <h1 th:text="${LoginData.userName}"></h1>
          <h1 th:text="${LoginData.email}"></h1>
          
    </body>
    </html>

saying error : Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'userName' cannot be found on null

Login page is not ruuning because LoginData is empty at begining

CodePudding user response:

You haven't exactly specified which of the 2 pages are causing the issue, but I'm going to guess that it is the first one.

I would first try replacing the th:value tag with th:field in the username input:

<input 
type="text"
name="userName" 

th:field="*{userName}"
id="exampleInputEmail1" 
aria-describedby="emailHelp">

Since you have already defined the loginData object within the form tag:

th:object="${loginData}

The next input tag could also cause some issues, I'm guessing that this should accept the user's password:

<input
type="email" 
name="email" 
th:value="${loginData}" 
 
id="exampleInputPassword1">

You would want to update it to something like:

<input
    type="password" 
    th:field="*{password}" 
     
    id="exampleInputPassword1">

CodePudding user response:

The exception tells you that there is no model attribute with the name loginData. looking at the controller code after this that can be confirmed as you are adding a model attribute with name login not loginData m.addAttribute("login", new LoginData());

Also I'm not usre why you define a loginData variable in your controller, check if it's non null and than don't use it but create a new one

LoginData loginData = new LoginData();
if(loginData != null ) {
    m.addAttribute("login", new LoginData());
    System.out.println("YAY");
}
  • Related