简体   繁体   中英

Vaadin Null Pointer Exception when trying to add component to tabsheet

I am still new to Java and Vaadin , and I am having a hard time trying to figure out how to resolve a null point exception when I try to populate the tabsheet with a component. Where am I screwing at? I know the issue is with my ObjList being null...but I am not sure how to resolve that. Please see below code:

Main Class:

public class Main extends Application {
    private ObjList objList = null;
    private ObjContainer dataSource = ObjContainer.createData();

private TabWindow tabWindow = new TabWindow(objList);

public void init() {
    buildmainlayout();
}

private void buildmainlayout() {
    setMainWindow(new Window("Test"));
    VerticalLayout layout = new VerticalLayout();
    layout.setSizeFull();
    layout.addComponent(tabWindow);
    getMainWindow().setConent(layout);
}

private TabWindow getTabWindow() {
    if(tabwindow == null) {
        objList = new ObjList(this);
        tabWindow = new TabWindow(objList);
    }
    return tabWindow;
}
public ObjContainer getDataSource() {
    return dataSource;
}
}

ObjList Class:

public class ObjList extends Table {
    public ObjList (Main app) {
        setSelectable(true);
        setImmediate(true);
        setContainerDataSource(app.getDataSource());
        setColumnHeaders(ObjContainer.COL_HEADERS_ENGLISH);
        setVisibleColumnsw(ObjContainer.NATURAL_COL_ORDER);
}
}

TabWindow Class:

public class TabWindow extends TabSheet implements TabSheet.SelectedTabChangeListener {
    private TabSheet t;
    public TabWindow(ObjList objList) {
        VerticalLayout l1 = new VerticalLayout();
        l1.setMargin(true);
        l1.addCOmponent(new Label("A"));
    t= new TabSheet();
    final Tab a = t.addTab(l1, "A", null));
    addComponent(t);
}
public void selectedTabChange(SelectedTabChangeEvent event){
    TabSheet tabsheet = event.getTabSheet();
    Tab tab = tabsheet.getTab(tabsheet.getSelectedTab());
    if (tab != null) {
         getWindow().showNotification("Selected tab" + tab.getCaption());
    }
 }
} 

Thanks!

I suspect the problem is due to how you are initializing the object graph in your implicit constructor for the Main class.

private ObjList objList = null;
private ObjContainer dataSource = ObjContainer.createData();

private TabWindow tabWindow = new TabWindow(objList);

When you create tabWindow and pass objList to the constructor it copies the reference to null . When you subsequently initialize the objList later in code it only affects that field specifically, not the reference inside tabWindow , as they are completely separate at that point. You need to rearrange the initialization logic somewhat to ensure that objList references something real before you pass it to another object.

All of this relates to how references are passed in Java, see this question for more on the topic: Is Java "pass-by-reference" or "pass-by-value"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM