call this in form returns null

Calling “this” inside the constructor (sometimes in functions too) of a class inherit from vaadin form it returns “null”.
Does it give a reason for this or is it a bug?

Can You provide some code snippet? I can’t believe what You said :slight_smile: because this is not related to Vaadin but Java itself.

Hello Radek,

public class VaadinForm extends Form {
//— GLOBAL VARIABLES ---------------------------------------

//— CONSTRUCTORS -------------------------------------------
public VaadinForm(){
System.out.println(“Form Konst:”+this);

HorizontalLayout hl = new HorizontalLayout();
hl.addComponent(new TextField("Eingabe:"));


the first line in constructor print null.
I think toString could be overwritten in form. It could return null.

You cannot reference “this” in a method - nor call instance methods nor reference instance fields - before calling the superclass constructor. After the Object() constructor call (whether implicit or explicit) at the top of the constructor hierarchy, “this” should be non-null from the point of view of that constructor if I remember correctly. However, other threads might not see “this” of the instance initialized before exiting the outermost constructor call.

What I suspect you are seeing is e.g. something like this: your constructor calls a superclass constructor, which calls a method that is overridden in the subclass and refers to an instance field that is initialized using “this”. That field is not yet initialized in the superclass constructor call, so it can look like “this” is null:

public abstract class Test1 {
    public Test1() {
    protected void printThis();

public class Test2 extends Test1 {
    private Test1 thisField = this;
    protected void printThis() {
        System.out.println(this);  // not null, but partly uninitialized
        System.out.println(thisField);  // null at this point

In any case, if “this” is actually null (and not e.g. something whose toString() yields null) during the constructor call, that is a Java issue, not a Vaadin issue.

There are also many articles about how initialization and constructor calls work in Java,
this one
looks at least reasonably complete based on a quick look.

I missed this reply when replying myself, even though I pointed at the possibility: toString() can certainly return null or “null” at this stage.