getUI returns null in a popup window

Trying to get the UI, using the getUI method in a component sitting into a popup window but also in a split button returns null.
Is it a bug.
Is there an alternative way ?
Tks

If your component is not attached to a proper hierarchy (before attach() for the component is called) getUI will return null.
Are you using getUI() in a constructor?
As an alternative you can pass th UI as a parameter or simply use the static method UI.getCurrent()

ciao

Claudio

Components are attached and the popup window is shown at the moment of the error but the getUI code, going back from a component to its parent don’t find the UI anchestor before it reaches the null parent.
It seems as if the popup window is not attached to UI in its anchestor chain,
The same happens in split buttons.
Unfortunately I can’t substitute the getUI because is not in my code but in the code of a Vaadin component : the FileDowdloader.
Tks

Nobody can help ?
Is it a bug ?
Tks

Here the popup code
package axioma.web.configuratore.ui.dialogs;

import java.io.;
import com.vaadin.server.
;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.ui.;
import com.vaadin.ui.Button.ClickEvent;
import axioma.rubik.csv.MatriciCsvImporter;
import axioma.web.configuratore.model.domain.TipiMatrici;
import axioma.web.template.ui.components.
;
import axioma.web.template.ui.dialogs.YesNoDialog;
import axioma.web.template.ui.layouts.HorizontalCaptionLayout;
import axioma.web.template.ui.messages.TemplateMessages;

public class MatrixImportDialog extends YesNoDialog {

private static final long serialVersionUID = 1L;

private DocumentFileReceiverField fileReceiver;
private TipiMatrici tipiMatrice;

public MatrixImportDialog(TipiMatrici xpMatrice) {
    super(TemplateMessages.CONFIRM_IMPORT.getText(), TemplateMessages.CONFIRM_IMPORT.getMessageCode(), null, null, new String[] {});
    tipiMatrice = xpMatrice;
}

@Override
public void createYesButton() {
    createYesButton(TemplateMessages.BUTTON_UPLOAD.getText(), TemplateMessages.BUTTON_UPLOAD.getMessageCode());
     addExtender();
}

private void addExtender() {
    DocumentDownloader downloader = new DocumentDownloader(null, "Import.csv") {

        private static final long serialVersionUID = 1L;

        @Override
        protected Resource buildResource() {
            return internalBuildResource();
        }
    };
    downloader.extend(btnYes);
}

private StreamResource internalBuildResource() {
    byte[] logData = importa();
    StreamSource source = () -> new ByteArrayInputStream(logData);
    StreamResource resource = new StreamResource(source, "Import.log");
    return resource;
}

@Override
public void show() {
    super.show();
}

@Override
public void createNoButton() {
    createNoButton(TemplateMessages.BUTTON_CLOSE.getText(), TemplateMessages.BUTTON_CLOSE.getMessageCode());
}

@Override
public Layout realBuildContent() {
    VerticalLayout vl = new VerticalLayout();
    vl.setSizeFull();
    HorizontalCaptionLayout hl = new HorizontalCaptionLayout(1);
    fileReceiver = new DocumentFileReceiverField("FileName");
    fileReceiver.setCaption("File Name");
    fileReceiver.setValue("");
    fileReceiver.setWidth("100%");
    hl.addComponent(fileReceiver);
    hl.setWidth("100%");
    vl.addComponent(hl);
    return vl;
}

public String getFileName() {
    return fileReceiver.getFileName();
}

public byte[] getContenuto() {
    return fileReceiver.getContent();
}

public DocumentFileReceiverField getFileReceiver() {
    return fileReceiver;
}

@Override
public void executeAction(ClickEvent xpEvent) {
}

public byte[] importa() {
    try {
        String nomeMatrice = tipiMatrice.getNome();
        MatriciCsvImporter importer = new MatriciCsvImporter(nomeMatrice);
        char[] contenuto = new String(getContenuto()).toCharArray();
        Reader reader = new CharArrayReader(contenuto);
        importer.setReader(reader);
        String logFileName = "Import_" + nomeMatrice + ".log";
        CharArrayWriter cwriter = new CharArrayWriter();
        Writer writer = new BufferedWriter(cwriter);
        importer.setLogWriter(writer);
        importer.setLogFileName(logFileName);
        importer.doImport();
        return new String(cwriter.toCharArray()).getBytes();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

}

DocumenDownloader is an extension of FileDownloader with the only relevant difference in :
public boolean handleConnectorRequest(VaadinRequest xpRequest, VaadinResponse xpResponse, String xpPath) throws IOException {
Resource res = buildResource();
setFileDownloadResource(res);
return super.handleConnectorRequest(xpRequest, xpResponse, xpPath)
}

BuildResource builds the needed resource at rutime.
Tks

Hi Tullio,
when you debug what is the last not null parent for the DocumentDownloader extension?

Can you post the source of YesNoDialog and an example the code you use to open the popup window?

Hi tullio, here is some code with standard Vaadin components that works for me (version 7.5.9)

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.FileDownloader;
import com.vaadin.server.StreamResource;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class Test_vaaUI extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = Test_vaaUI.class)
    public static class Servlet extends VaadinServlet { }
    
    @Override protected void init(VaadinRequest request) {
        addWindow(new Window() {
            {
                setCaption("Download window");
                setModal(true);
                center();
                setWidth("20em");
                setHeight("10em");
                
                Button download = new Button("download");
                
                StreamSource s = new StreamSource() {
                    @Override public InputStream getStream() { return new ByteArrayInputStream("1,2\n3,4".getBytes()); }
                };
                
                StreamResource resource = new StreamResource(s, "test.csv");
                resource.setCacheTime(0);
                
                FileDownloader fileDownloader = new FileDownloader(resource);
                fileDownloader.extend(download);
                
                VerticalLayout l = new VerticalLayout(download);
                l.setComponentAlignment(download, Alignment.MIDDLE_CENTER);
                l.setSizeFull();
                setContent(l);
            }
        });
    }
}

Hope it helps