Swing库中
AbstractButton类中
protected ButtonModel model= null;
public ButtonModel getModel() {
return model;
public void setActionCommand(String actionCommand) {
getModel().setActionCommand(actionCommand);//getModel反回了一个空指针的接口对象,并调用了其中预定义的方法。
}
ButtonModel不是类是接口:
public interface ButtonModel extends ItemSelectable
public void setActionCommand(String s);//接口中的确预定义好了setActionCommand方法。
我自己模仿这种方式建了测试类,发现会有 java.lang.NullPointerException空指针异常,那么这段代码是什么意思,又为了实现什么需要这么操作啊。