0

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!

  • when there is an exception there usually is a also a backtrace, which should give you pretty good startpoint to look for the problems (ideally in the debugger of an IDE). so at least show us your stacktrace and the exception. – cfrick Aug 15 '14 at 13:56

1 Answers1

1

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"?

Community
  • 1
  • 1
18grady
  • 51
  • 4
  • I think I figured it out thanks to your help: I added `private Panel panel = new Panel();` then changed `private TabWindow tabWindow = new Tabwindow(objList);` to `private TabWindow tabWindow = null;` and then added `private void setMainComponent (Component tabWindow) { panel.addComponent(TabWindow):` and it worked! Thank you! – ghostFishKillah Aug 15 '14 at 16:15