Customised WebLookupField, setOptionsList() doesnot set the values

I have replaced the standard LookupField implementation with my own.
Implementation :

  1. Created “CustomLookupField” class named “NetworkSearchComponent” 56

2.Created components.xml file in web module:

   <components xmlns="http://schemas.haulmont.com/cuba/components.xsd">
   <component>
        <name>LookupField</name>
        <class>com.company.zeusv2.web.screens.NetworkSearchComponent</class>
        <tag>LookupField</tag>
     <componentLoader>com.haulmont.cuba.gui.xml.layout.loaders.LookupFieldLoader</componentLoader>
    </component>
</components>
  1. Registered this file in web-app.properties :

cuba.web.componentsConfig = +com/company/demo/components.xml

Problem:
I am not able to set options to this component. However other properties like “setWidth“ “setCaption“ “setValues“ are able to set. If I try to “this.setOptionsList(Arrays.asList(“Option1”, “Option2”));” as shown in fig 1. I am getting this null pointer exception.

java.lang.NullPointerException
at com.haulmont.cuba.web.gui.components.WebLookupField.setOptions(WebLookupField.java:242)
at com.haulmont.cuba.gui.components.OptionsField.setOptionsList(OptionsField.java:61)
at com.company.zeusv2.web.screens.NetworkSearchComponent.(NetworkSearchComponent.java:27)
at com.company.zeusv2.web.networkmapping.NetworkMappingBrowse.init(NetworkMappingBrowse.java:50)
at com.haulmont.cuba.gui.components.AbstractWindow.init(AbstractWindow.java:96)
at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
at com.haulmont.cuba.gui.screen.Screen.fireEvent(Screen.java:128)
at com.haulmont.cuba.gui.screen.UiControllerUtils.fireEvent(UiControllerUtils.java:58)
at com.haulmont.cuba.web.sys.WebScreens.createScreen(WebScreens.java:248)
at com.haulmont.cuba.web.sys.WebScreens.create(WebScreens.java:171)
at com.haulmont.cuba.gui.config.MenuItemCommands$ScreenCommand.run(MenuItemCommands.java:212)
at com.haulmont.cuba.web.sys.SideMenuBuilder$MenuCommandExecutor.accept(SideMenuBuilder.java:264)
at com.haulmont.cuba.web.sys.SideMenuBuilder$MenuCommandExecutor.accept(SideMenuBuilder.java:249)
at com.haulmont.cuba.web.gui.components.mainwindow.WebSideMenu$MenuItemImpl.menuSelected(WebSideMenu.java:569)
at com.haulmont.cuba.web.widgets.CubaSideMenu$1.menuItemTriggered(CubaSideMenu.java:90)
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 com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:153)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:115)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:431)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:396)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:260)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:82)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1577)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:425)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:329)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:215)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:107)

I also tried overriding this method, instead of setting this property in the constructor but its not working.

Please provide your inputs how to setOptionsList to this component.

Hi,

At the time the constructor is invoked no beans are injected. The setOptions method uses beanLocator, so that you get NPE. You need to override the afterPropertiesSet method and provide you custom initialization there (do not forget to call super).

Gleb

Thankyou for the input.

48

I have implemented the initialization on afterPropertiesSet method but still getting NPE. Also setCaption property is not working.

Regards,
Vedika

Could you please attach a demo project where the problem is reproduced?

Gleb

Hello Gelb,
I have attached this sample project.I have created a component “NetworkSearchComponent”. Displaying this component on CustomerBrowse. I have set values on afterPropertiesSet(). but still not displaying data.

47

sample-sales-cuba7.zip (2.8 MB)

Please provide your input.

You create a component using a constructor, but as the warning says, you need to use the UiComponents bean, otherwise, no component beans are initialized and afterPropertiesSet isn’t invoked. That’s why you see no options and no caption.

01

I recommend completely define a new component:

components.xml

<components xmlns="http://schemas.haulmont.com/cuba/components.xsd">
    <component>
        <name>networkSearchComponent</name>
        <class>com.company.sales.web.NetworkSearchComponent</class>
        <componentLoader>com.company.sales.web.xml.layout.loaders.NetworkSearchComponentLoader</componentLoader>
    </component>

</components>

web-app.properties

cuba.web.componentsConfig = +com/company/sales/web/components.xml

NetworkSearchComponent.java

public class NetworkSearchComponent extends WebLookupField<String> {

    public static final String NAME = "networkSearchComponent";

    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();

        this.setCaption("Network");
        this.setOptionsList(Arrays.asList("Option1", "Option2"));
    }
}

NetworkSearchComponentLoader.java

public class NetworkSearchComponentLoader extends LookupFieldLoader {

    @Override
    public void createComponent() {
        resultComponent = factory.create(NetworkSearchComponent.NAME);
        loadId(resultComponent, element);
    }
}

CustomerBrowser.java

@Subscribe
protected void onInit(InitEvent event) {
    ntwk = uiComponents.create(NetworkSearchComponent.class);
    groupbox.add(ntwk);
}

Gleb

1 Like

Thank-you for looking into the problem.
It worked!

-Vedika