NPE in VaadinSession.hasLock() after upgrading from Vaadin 7.0.6 to 7.3.2

I’m trying to upgrade my app from Vaadin 7.0.6 to 7.3.2 because of some Progress Indicator polling issues (I see they are discussed here https://vaadin.com/forum#!/thread/2015353 and I hope that upgrading to the newer Vaadin version will help).

code:

   [code]

labelProperty = new MethodProperty(config, “label”)
TextField descriptionField = new TextField();
descriptionField.addStyleName(“server-panel-textfield”);
descriptionField.setPropertyDataSource(labelProperty);
[/code]

error:

[code]
java.lang.NullPointerException
at com.vaadin.server.VaadinSession.hasLock(VaadinSession.java:820)
at com.vaadin.server.VaadinSession.getConverterFactory(VaadinSession.java:635)
at com.vaadin.data.util.converter.ConverterUtil.getConverter(ConverterUtil.java:54)
at com.vaadin.ui.AbstractField.setConverter(AbstractField.java:676)
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:623)

I see that UI is null:
protected VaadinSession getSession() {
UI uI = getUI();
[/code]I see that UI is null in AbstractClientConnector:

 protected VaadinSession getSession() {
    UI uI = getUI();

so I guess I need to add/set some UI in unit tests, but this test used to work fine with Vaadin 7.0.6…

Could you elaborate where you’re getting the NPE with the provided stack trace? Apparently in a unit test? You seem to have a VaadinSession there but the problem is that its lock instance is null. Recent Vaadin versions assert in almost every VaadinSession method that the session is locked; in 7.0.6 this particular assert did not exist and thus no NPE was thrown either. In unit tests, you can use the following subclass to ensure things go smoothly:

public class TestVaadinSession extends VaadinSession {

    public TestVaadinSession(VaadinService service) {
        super(service);
        lock();
    }

    @Override
    public Lock getLockInstance() {
        return lock;
    }

    private ReentrantLock lock = new ReentrantLock();
}

thank you, this worked.

I created this helper, which should probably in vaadin libraries somewhere…

public class VaadinTestHelper {
    public static void initVaadinSession(Class clazz) {
        try {
            VaadinSession.setCurrent(new TestVaadinSession(new VaadinServletService(
                    new VaadinServlet(), new DefaultDeploymentConfiguration(
                    clazz, new Properties()))));
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    static class TestVaadinSession extends VaadinSession {

        public TestVaadinSession(VaadinService service) {
            super(service);
            lock();
        }

        @Override
        public Lock getLockInstance() {
            return lock;
        }

        private ReentrantLock lock = new ReentrantLock();
    }
}