添加ViewButtons
类时,在尝试引用NullPointerException
时会引发actionMap
。删除ViewButtons
类并取消注释下面的代码可以解决问题。但是,我正在尝试分离我的代码,因为应用程序将扩展。如何在不生成actionMap
的情况下引用ViewButtons
中的NullPointerException
?
public class View extends FrameView {
private javax.swing.JButton saveButton;
private javax.swing.JPanel mainPanel;
private javax.swing.JPanel tableContainer
public View(SingleFrameApplication app) {
super(app);
initComponents();
}
private void initComponents() {
mainPanel = new javax.swing.JPanel();
tableContainer = new JPanel();
saveButton = new javax.swing.JButton();
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(MyApp.class).getContext().getActionMap(View.class, this);
ViewButtons viewButtons = new ViewButtons(actionMap);
mainPanel.setName("mainPanel"); // NOI18N
mainPanel.setLayout(new BorderLayout());
tableContainer.setName("tableContainer");
tableContainer.setLayout(new BorderLayout());
// buttonPanel = new JPanel();
// buttonPanel.setMinimumSize(new Dimension(800, 30));
// buttonPanel.setName("buttonPanel");
// saveButton.setAction(actionMap.get("save")); // NOI18N
// saveButton.setName("saveButton"); // NOI18N
// buttonPanel.add(saveButton);
// tableContainer.add(buttonPanel, BorderLayout.PAGE_END);
tableContainer.add(viewButtons.getMainComponent(), BorderLayout.PAGE_END);
setComponent(mainPanel);
}
class ViewButtons {
private JPanel buttonPanel = new JPanel();
public ViewButtons(ActionMap actionMap) {
buttonPanel.setMinimumSize(new Dimension(800, 30));
buttonPanel.setName("buttonPanel");
saveButton.setAction(actionMap.get("save")); <<<<- NULLPONTER EXCEPTION
saveButton.setName("saveButton"); // NOI18N
buttonPanel.add(saveButton);
}
public JComponent getMainComponent() {
return buttonPanel;
}
}