0

Refer the payloads 1 and 2 below, "fy" fields are absent in the payload1. I tried with if else conditions, but no luck, still I am getting error. Is there a way to check object present or not. Refer the error below. Property or field 'numBrothers' cannot be found on null

// Payload for id1
 "fi": {
        "fv": "Traditional",
        "ft": "Nuclear Family",
        "fs": "Upper Middle Class",
        "fn": "Basker",
        "fi": "Mr.",
        "fnd": "Bangalore",
        "mn": "Kani",
        "mi": "Mrs."
      }
    // Payload for id2
    "fi": {
    "fv": "Liberal",
    "fs": "Upper Middle Class",
    "fn": "Esaw",
    "fi": "Mr.",
    "fnd": "Coimbatore",
    "mn": "Shee",
    "mi": "Mrs.",
    "mo": "Professional",
    "fy": {
      "nb": 0,
      "mb": 0,
      "ns": 1,
      "ms": 0
    }
  }
      

Error Log:

2021-02-24 16:40:19.427 ERROR 1833 --- [nio-5555-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/matrimony] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/profile.html]")] with root cause

**org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'numBrothers' cannot be found on null
        at** org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.3.4.jar:5.3.4]

// Sample code

 <td>
 <div th:if= "${profile.familyInfo.family != null && profile.familyInfo.family.numBrothers!= null}" />
    
                         <span th:text="${profile.familyInfo.family.numBrothers}" />
    
                         </div>
    
                         <div th:unless= "${profile.familyInfo.family != null && profile.familyInfo.family.numBrothers!= null}">Not Specified</span>
    </div>          
    </td>
selvi
  • 1,271
  • 2
  • 21
  • 41
  • Does this answer your question? [Using Thymeleaf when the value is null](https://stackoverflow.com/questions/20636456/using-thymeleaf-when-the-value-is-null) – Jakub Ch. Feb 24 '21 at 22:13
  • 1
    You are probably looking for [safe navigation operator](https://stackoverflow.com/a/41831556/5684265) – Jakub Ch. Feb 24 '21 at 22:14

0 Answers0