HTTP Status 500 - Request processing failed;

698 views Asked by At

This is my model of consent

    @Entity
@Table(name = "consents")
public class Consent extends BaseEntity {

    /**

     */
    @OneToOne
    @JoinColumn(name = "provider_id")
    private Provider provider;

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;

    @OneToOne
    @JoinColumn(name = "friend_id")
    private User friend;


    @OneToOne
    @JoinColumn(name = "document_id")
    private Document document;


    @Column(name = "status")
    private String status;

    /**
     * C
     * @return 
     */
    public void Provider() {
    }

    public Provider getProvider() {
        return this.provider;
    }

    /**
     * 
     * Setter for property pet.
     *
     * @param pet New value of property pet.
     *
     */

    public void setProvider(Provider provider) {
        this.provider = provider;
    }


    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }


    public User getFriend() {
        return friend;
    }


    public void setFriend(User friend) {
        this.friend = friend;
    }


    public Document getDocument() {
        return document;
    }


    public void setDocument(Document document) {
        this.document = document;
    }


    public String getStatus() {
        return status;
    }


    public void setStatus(String status) {
        this.status = status;
    }



}

This is the consent controller get declaration

@RequestMapping(value = "/users/{userId}/providers/{providerId}/documents/{documentId}/consents/new", method = RequestMethod.GET)
public String initNewConsentForm(@PathVariable("userId") int userId,@PathVariable("providerId") int providerId,@PathVariable("documentId") int documentId, Model model) {
    User user = this.clinicService.findUserById(userId);
    Provider provider = this.clinicService.findProviderById(providerId);
    Document document = this.clinicService.findDocumentById(documentId);
    Collection<User> users = this.clinicService.AllUsers();



    Consent consent = new Consent();
        //document.addConsent(consent);
        //provider.addDocument(document);
        //user.addProvider(provider);
        model.addAttribute("provider",provider);
        model.addAttribute("document", document);
        model.addAttribute("user", user);
        model.addAttribute("users", users);
        model.addAttribute("consent", consent);
        return "providers/createOrUpdateConsentForm";

}

this is consent post declaration

 @RequestMapping(value = "/users/{userId}/providers/{providerId}/documents/{documentId}/consents/new", method = RequestMethod.POST)
public String processNewConsentForm(@PathVariable("userId") int userId, @PathVariable("providerId") int providerId, 
        @PathVariable("documentId") int documentId, @ModelAttribute("consent") Consent consent, BindingResult result, SessionStatus status) {


    User user = this.clinicService.findUserById(userId);
    Provider provider = this.clinicService.findProviderById(providerId);
    //System.out.println("daghade");
    //System.out.println(provider);
    //Document doc = this.clinicService.findDocumentById(userId);

     Consent c =new Consent();

        c.setProvider(consent.getProvider());
        c.setDocument(consent.getDocument());
        c.setUser(user);
        c.setStatus(consent.getStatus());
        c.setFriend(consent.getFriend());
        System.out.print("consentcontroller11");
        System.out.println(consent.getProvider());
        System.out.print("consentcontroller11");
    if (result.hasErrors()) {
        return "providers/createOrUpdateConsentForm";
    } else {
        this.clinicService.saveConsent(c);
        status.setComplete();
    return "redirect:/users/{userId}";
}

}

the consent html thymeleaf form is

                          <form th:object="${consent}" action="../users/userDetails.html" th:action="@{${#httpServletRequest.servletPath}}" method="post">
                             <fieldset>
                                        <div class="col-sm-7 col-md-6 col-lg-5">
                                        <label for="last_name">Service  Provider</label>
                                        <select th:field="*{document.provider.id}" name="provider" id="provider" class="form-control" th:onchange="'javascript:showPIIDoc(this.value);'">
                                                <option th:value="0" >Select a Service Provider</option>
                                                <option th:each="provider : ${user.providers}"  th:value="${user.id} +','+ ${provider.id}" th:text="${provider.name}" >[name]</option>
                                            </select>

                                        </div>  
                                        <div style="clear:both"></div>
                                        <div class="col-sm-7 col-md-6 col-lg-5">
                                        <label for="last_name">PII Document</label>
                                            <select th:field="*{document.id}"  id ="document_id"  class="form-control">

                                            </select>
                                        </div>  
                                        <div style="clear:both"></div>

                                            <div class="col-sm-7 col-md-6 col-lg-5">
                                        <label for="last_name">Share with</label>
                                            <select th:field="*{user.id}" id="friend_id"  class="form-control">
                                                <option th:value="0" >Select a user you want share the document to</option>
                                                <option name="name" th:each="user : ${users}" th:value="${user.id}" th:text="${user.firstName} + ' ' + ${user.lastName}">[name]</option>
                                            </select>
                                        </div>
                                        <div style="clear:both"></div>
                                        <div class="col-sm-7 col-md-6 col-lg-5">
                                            <label for="last_name">Consent</label>
                                            <div style="clear:both"></div>
                                            <input type="checkbox" id="status" th:field="*{status}" name="share" th:value="1" th:text="Share" />
                                         </div> 

                                         <div style="clear:both"></div>

                            <div style="margin-top:10px;margin-left:10px" class="form-actions">
                                <button class="btn btn-primary" type="submit">Add Consent</button>

                            </div>
                          </fieldset>
                        </form>

After submitting the form. I get this error

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "user.providers" (providers/createOrUpdateConsentForm

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'providers' cannot be found on null

My User Class:

    @Entity
@Table(name = "users")
public class User extends Person {
    @Column(name = "gender")
    @NotEmpty
    private String gender;


    @Column(name = "birth_date")
    private String birthDate;

    @Column(name = "email")
    @NotEmpty
    private String email;

    @Column(name = "username")
    @NotEmpty
/*    @Digits(fraction = 0, integer = 10)*/
    private String username;

    @Column(name = "password")
    @NotEmpty
/*    @Digits(fraction = 0, integer = 10)*/
    private String password;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Provider> providers;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Document> documents;

    @JsonManagedReference
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
    private Role role;

    public void setProviders(Set<Provider> providers) {
        this.providers = providers;
    }
    public void setDocuments(Set<Document> documents) {
        this.documents = documents;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    protected void setProvidersInternal(Set<Provider> providers) {
        this.providers = providers;
    }

    protected void setDocumentsInternal(Set<Document> documents) {
        this.documents = documents;
    }


    protected Set<Provider> getProvidersInternal() {
        if (this.providers == null) {
            this.providers = new HashSet<Provider>();
        }
        return this.providers;
    }


    protected Set<Document> getDocumentsInternal() {

        if (this.documents == null) {
            this.documents = new HashSet<Document>();
        }
        return this.documents;
    }

    public List<Provider> getProviders() {
        List<Provider> sortedProviders = new ArrayList<Provider>(getProvidersInternal());
        PropertyComparator.sort(sortedProviders, new MutableSortDefinition("name", true, true));
        System.out.print("sortedProvider");
        System.out.print(sortedProviders);

        return Collections.unmodifiableList(sortedProviders);
    }

    public List<Document> getDocuments() {
        List<Document> sortedDocuments = new ArrayList<Document>(getDocumentsInternal());
        PropertyComparator.sort(sortedDocuments, new MutableSortDefinition("name", true, true));
        System.out.print("sortedDocuments");
        System.out.print(sortedDocuments);
        return Collections.unmodifiableList(sortedDocuments);
    }


    public void addProvider(Provider provider) {
        getProvidersInternal().add(provider);
        provider.setUser(this);
    }

    public void addDocument(Document document, Provider provider) {
        getDocumentsInternal().add(document);
        System.out.print("addDocument");
        System.out.print(document);
        document.setUser(this);
        document.setProvider(provider);
        System.out.print("addDocument");
        System.out.print(document);

    }
    /**
     * Return the Pet with the given name, or null if none found for this Owner.
     *
     * @param name to test
     * @return true if pet name is already in use
     */
    public Provider getProvider(String name) {
        return getProvider(name, false);
    }

    public Document getDocument(String name) {
        return getDocument(name, false);
    }

    /**
     * Return the Pet with the given name, or null if none found for this Owner.
     *
     * @param name to test
     * @return true if pet name is already in use
     */
    public Provider getProvider(String name, boolean ignoreNew) {
        name = name.toLowerCase();
        for (Provider provider : getProvidersInternal()) {
            if (!ignoreNew || !provider.isNew()) {
                String compName = provider.getName();
                compName = compName.toLowerCase();
                if (compName.equals(name)) {
                    return provider;
                }
            }
        }
        return null;
    }

    public Document getDocument(String name, boolean ignoreNew) {
        name = name.toLowerCase();
        for (Document document : getDocumentsInternal()) {
            if (!ignoreNew || !document.isNew()) {
                String compName = document.getName();
                compName = compName.toLowerCase();
                if (compName.equals(name)) {
                    return document;
                }
            }
        }
        return null;
    }



    public Role getRole() {
        return role;
    }

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

    @Override
    public String toString() {
        return new ToStringCreator(this)

                .append("id", this.getId())
                .append("new", this.isNew())
                .append("lastName", this.getLastName())
                .append("firstName", this.getFirstName())
                .append("gender", this.gender)
                .append("bithDate", this.birthDate)
                .append("email", this.email)
                .append("username", this.username)
                .append("password", this.password)
                .toString();
    }
}
1

There are 1 answers

2
Faraj Farook On BEST ANSWER

Error message tells that the user model attribute is null

in your post request mapping add the model attributes as below

@RequestMapping(value = "/users/{userId}/providers/{providerId}/documents/{documentId}/consents/new", method = RequestMethod.POST)
public String processNewConsentForm(Model model, @PathVariable("userId") int userId, @PathVariable("providerId") int providerId, 
        @PathVariable("documentId") int documentId, @ModelAttribute("consent") Consent consent, BindingResult result, SessionStatus status) {

    ...
    if (result.hasErrors()) {

        User user = this.clinicService.findUserById(userId);
        Provider provider = this.clinicService.findProviderById(providerId);
        Document document = this.clinicService.findDocumentById(documentId);
        Collection<User> users = this.clinicService.AllUsers();

        model.addAttribute("provider",provider);
        model.addAttribute("document", document);
        model.addAttribute("user", user);
        model.addAttribute("users", users);
        model.addAttribute("consent", consent);

        return "providers/createOrUpdateConsentForm";
    } 
    ...
}