Java 重构后引用ActionMap的NullPointerException
添加了Java 重构后引用ActionMap的NullPointerException,java,nullpointerexception,action,Java,Nullpointerexception,Action,添加了ViewButtonsclass aNullPointerException后,在尝试引用actionMap时会引发异常。删除ViewButtons类并取消注释下面的代码可以解决问题。但是,由于应用程序即将展开,我正在尝试分离代码。如何从ViewButtons引用actionMap,而不生成NullPointerException public class View extends FrameView { private javax.swing.JButton saveButton; p
ViewButtons
class aNullPointerException
后,在尝试引用actionMap
时会引发异常。删除ViewButtons
类并取消注释下面的代码可以解决问题。但是,由于应用程序即将展开,我正在尝试分离代码。如何从ViewButtons
引用actionMap
,而不生成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;
}
}
public类视图扩展了FrameView{
私有javax.swing.JButton saveButton;
私有javax.swing.JPanel主面板;
私有javax.swing.JPanel tableContainer
公共视图(SingleFrameApplication应用程序){
超级(app);
初始化组件();
}
私有组件(){
mainPanel=newjavax.swing.JPanel();
tableContainer=新的JPanel();
saveButton=newjavax.swing.JButton();
javax.swing.ActionMap ActionMap=org.jdesktop.application.application.getInstance(MyApp.class).getContext().getActionMap(View.class,this);
ViewButtons ViewButtons=新的ViewButtons(actionMap);
mainPanel.setName(“mainPanel”);//NOI18N
setLayout(新的BorderLayout());
tableContainer.setName(“tableContainer”);
setLayout(新的BorderLayout());
//buttonPanel=新的JPanel();
//按钮面板设置最小尺寸(新尺寸(800,30));
//buttonPanel.setName(“buttonPanel”);
//saveButton.setAction(actionMap.get(“save”);//NOI18N
//saveButton.setName(“saveButton”);//NOI18N
//buttonPanel.add(保存按钮);
//tableContainer.add(按钮面板,边框布局,第页结束);
tableContainer.add(viewButtons.getMainComponent(),BorderLayout.PAGE_END);
设置组件(主面板);
}
类视图按钮{
private JPanel buttonPanel=new JPanel();
公共视图按钮(ActionMap ActionMap){
按钮面板设置最小尺寸(新尺寸(800,30));
buttonPanel.setName(“buttonPanel”);
saveButton.setAction(actionMap.get(“保存”));这是swing吗?您的actionMap
是以静态方式获得的。@nachokk正确。这是swing您发布的代码不完整,例如saveButton
的定义缺失,可能为空。@Njol根据请求添加了实例变量。@newojava如果这是您的全部代码,那么问题在于saveButton
为null,或者如果您仍然缺少代码,那么我假设它设置在new ViewButtons(actionMap);
下面,也就是说,调用该构造函数时它为null,导致NPE。