有一个基类ServerAdapter
:
public abstract class ServerAdapter {
public ServerAdapter() {
initGUI();
}
protected abstract void initGUI();
}
还有一个继承的子类ServerAdapter
:
public abstract class LinuxServerAdapter extends ServerAdapter {
protected CheckBox key = new CheckBox();
public LinuxServerAdapter() {
super();
}
@Override
public void initGUI() {
//NPE is thrown here, because key = null
key.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
//Something happens here
}
});
}
}
继承 LinuxServerAdapter 的端类:
public class MyLinuxServerAdapter extends LinuxServerAdapter {
public MyLinuxServerAdapter() {
super();
}
public static void main(String args[]) {
ServerAdapter server = new MyLinuxServerAdapter();
}
}
当我尝试在键上添加 clickHandler 时,会引发 NPE。
为什么key
没有初始化?这是初始化顺序以特定方式工作的情况吗?