0

this is my first time posting here. I have a question regarding on how to save the data coming from "html select multiple" into the database. I am using dto(data transfer object) to pass the value of the inputs in my form to the controller.

This is my HTML CODE:

<form:form id="user-form" action="/admin/users/add-user/add-success" commandName="addUser" method="POST">
    <fieldset>
        <legend class="text-center">
            <label class="text font-md">Profile Information</label>
        </legend>             

        <div class="form-group">
            <label class="control-label">Select Property <span class="important">*</span></label>
            <select class="form-control custom-scroll" path="propertyList.propertyId" name="propertyId" id="propertyId" multiple>
                <c:forEach var="property" items="${property}">
                    <option value="${property.id}" data-value="${property.prtCompany.id}"><c:out value="${property.propertyName}"/></option>
                </c:forEach>
            </select>
            <div class="note">
                <strong>Note:</strong> hold down the ctrl/cmd button to select multiple options.
            </div>
        </div>
        <div class="form-actions">
            <div class="row">
                <div class="col-sm-12">
                    <button class="btn btn-primary" type="submit">
                        Submit
                    </button>
                </div>
            </div>
        </div>
    </fieldset>
</form:form>

This is the code of my Controller:

@RequestMapping(value = "/admin/users/add-user/add-success", method = RequestMethod.POST)
public ModelAndView saveAddUser(ModelMap mvc, @ModelAttribute("addUser") UserProfileDto userProfileDto, HttpServletResponse response) throws IOException, ServerException {

    ModelAndView mv = new ModelAndView("user-pages/user");

    userProfileDto.setAuditId(auditService.logAuditCreate().getId());
    userProfileDto = userProfileService.createUserProfile(userProfileDto);

    return mv;
}

This is my ServiceImpl:

public UserProfileDto createUserProfile(UserProfileDto userProfileDto) {
    UserProfile userProfile = new UserProfile();
    userProfile = convertDtoToEntity(userProfile, userProfileDto);
    save(userProfile);
    return convertEntityToDto(userProfile);
}

protected UserProfile convertDtoToEntity(UserProfile userProfile, UserProfileDto dto) {

List<Property> propertyList = listUsersProperty(dto.getPropertyList());

userProfile.setProperty(propertyList);

return userProfile;
}

protected List<Property> listUsersProperty(List<PropertyDto> propertyDtoList) {
List<Property> propertyList = null;
for (PropertyDto propertyDto : propertyDtoList) {
    propertyList.add(entityManager.find(Property.class, propertyDto.getId()));
}
return propertyList;
}

This is the error:

19:37:37,543 ERROR [io.undertow.request] (default task-40) UT005023: Exception handling request to /admin/users/add-user/add-success: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.ejb.EJBException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.altypaynet.crm.service.user.UserProfileServiceImpl.listUsersProperty(UserProfileServiceImpl.java:249)
at com.altypaynet.crm.service.user.UserProfileServiceImpl.convertDtoToEntity(UserProfileServiceImpl.java:261)
at com.altypaynet.crm.service.user.UserProfileServiceImpl.createUserProfile(UserProfileServiceImpl.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82)
at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93)
at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)
... 91 more

And the DTO's,

UserProfileDto:

public class UserProfileDto extends UserBaseDto {

public UserProfileDto() {
}

private List<PropertyDto> propertyList;

public List<PropertyDto> getPropertyList() {
    return propertyList;
}

public void setPropertyList(List<PropertyDto> propertyList) {
    this.propertyList = propertyList;
}
}

PropertyDto:

public class PropertyDto extends CompanyDto {

public PropertyDto() {
}

private String propertyId;

private String propertyNo;

private String propertyName;

private String propertyType;

private Integer propertyUnits;

private Integer propertyFloors;

public String getPropertyId() {
    return propertyId;
}

public void setPropertyId(String propertyId) {
    this.propertyId = propertyId;
}

public String getPropertyNo() {
    return propertyNo;
}

public void setPropertyNo(String propertyNo) {
    this.propertyNo = propertyNo;
}

public String getPropertyName() {
    return propertyName;
}

public void setPropertyName(String propertyName) {
    this.propertyName = propertyName;
}

public String getPropertyType() {
    return propertyType;
}

public void setPropertyType(String propertyType) {
    this.propertyType = propertyType;
}

public Integer getPropertyUnits() {
    return propertyUnits;
}

public void setPropertyUnits(Integer propertyUnits) {
    this.propertyUnits = propertyUnits;
}

public Integer getPropertyFloors() {
    return propertyFloors;
}

public void setPropertyFloors(Integer propertyFloors) {
    this.propertyFloors = propertyFloors;
}
}

I already try searching about this but unfortunately there is no one that solve my problem. I hope you can help me guys. thanks.

note: Maybe some of you may ask what is the relation of the property to userprofile, I remove some of the code that are included in the codes, i just post the codes that is related to the error.

BIG NOTE: SORRY FOR MY BAD ENGLISH. I DID MY BEST.

Royts
  • 501
  • 6
  • 14
  • You never initialize your "propertyList" to anything other than null (List propertyList = null;). So obviously you will get a NPE when trying to call propertyList.add(...) – OH GOD SPIDERS Oct 13 '16 at 12:42
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Oct 13 '16 at 12:42
  • @911DidBush List propertyList = null; for (PropertyDto propertyDto : propertyDtoList) { propertyList.add(entityManager.find(Property.class, propertyDto.getId())); } return propertyList; After it goes to for each it should not return null. – Royts Oct 13 '16 at 12:44
  • Please read the Question I linked. You cannot call propertyList.add as long as propertyList is set to null. – OH GOD SPIDERS Oct 13 '16 at 12:47
  • I also tried using List propertyList = new ArrayList();, do you have something to reccomend more? Anyways, thanks for noticing my question. – Royts Oct 13 '16 at 12:52
  • Okay, if you initialize the propertyList like than you remove one source of a NPE. You should also check if the propertyDtoList is null before you iterate over it in a for loop, as using an enhanced for loop on a null object will also throw a NullPointerException. [if (propertyDtoList!=null) { for (PropertyDto propertyDto : propertyDtoList) {...}}] – OH GOD SPIDERS Oct 13 '16 at 13:00
  • @911DidBush Okay Bro, I'll try your suggestion. Many thanks. – Royts Oct 13 '16 at 13:02
  • Please Note that this will only help remove the NullPointerExceptions (Errors) that you are getting. There might still be logical errors in the code (Like for example the DTO not holding the values that it should). – OH GOD SPIDERS Oct 13 '16 at 13:06

1 Answers1

0

Instead of UserProfileDto bean pass PropertyDto to the controller.

PropertyDto.getProperId() will give you all the selected values seprated by comma. You are getting null pointer as dto.getPropertyList() does not contain any value.

himanshu saini
  • 123
  • 1
  • 1
  • 9
  • Thanks for your answer, but I cannot do that, as I said on the last part of my question, there are other code that is included to that, I just remove it because the error I am getting is in the select multiple only – Royts Oct 13 '16 at 12:55
  • in other words, i have other inputs that needs to be bind in UserProfileDto, I just remove it the codes I give. – Royts Oct 13 '16 at 12:56
  • Then you at least need to add private String propertyId in your UserProfileDto alongwith getter setters. – himanshu saini Oct 13 '16 at 13:04
  • Instead of private String propertyIid, I call the List of PropertyDto by initializing it. – Royts Oct 13 '16 at 13:07
  • this line: private List propertyList; – Royts Oct 13 '16 at 13:07
  • This way you wont get the value of multi select. Try to understand basics of java framework. Name of your multi select is propertyId, So you need to pass a bean that contains a variable with the same name. Only then you will be able to get the value. – himanshu saini Oct 13 '16 at 13:23
  • Initializing arrayList wont solve your problem. Wont recieve multiselects value. PropertyList is an arraylist of type PropertyDto. It wont get multiselects value. – himanshu saini Oct 13 '16 at 13:28