我有一个启动程序的runApplication类:
public class runApplication {
private MainFrame mainFrame;
private Starter starterModel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
runApp();
}
});
}
public static void runApp() {
new Controller();
}
}
它调用控制器:
public class Controller implements FormListener {
private FunctionTypeOrg fto;
private MatrixTableModel matrixTabelmodel;
private MainFrame mainFrame;
private Integer numberOfFuncs;
private Integer numberOfOrgs;
private FormPanel formPanel;
private String path;
private ParsFile parsFile;
private TablePanel tablePanel;
public void setPath(String path) {
this.path = path;
}
public Integer getNumberOfFuncs() {
return numberOfFuncs;
}
public Integer getNumberOfOrgs() {
return numberOfOrgs;
}
public String getPath() {
return path;
}
public Controller() {
this.mainFrame = new MainFrame();
this.tablePanel = new TablePanel();
}
public void runPars (String path){
this.parsFile = new ParsFile();
parsFile.parsingData(path);
this.numberOfFuncs = new Integer(parsFile.getCountAufgabe());
this.numberOfOrgs = new Integer(parsFile.getCountOrganisationseinheit());
}
在MainFrame类中,我有这个方法从开放菜单中获取要处理的文件的路径:
///////Open Menu Item//////////
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (fileChooser.showOpenDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
String path = fileChooser.getSelectedFile().toString();
//controller.setPath(path);
formPanel.setPath(path);
}
else{System.out.println("no Path");}
}
});
MainFrame将路径发送到formPanel。现在,FormPanel在控制器中调用runPars方法,它应该在Model中运行另一个方法并获取结果。
FormPanel中:
// ////Draw Button///////
drawBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (path == null) {
JOptionPane.showMessageDialog(drawBtn,
"No Data is selected", "Choose Data",
JOptionPane.ERROR_MESSAGE);
} else {
controller.runPars(path);
String orgStr = controller.getNumberOfOrgs().toString();
String funcStr = controller.getNumberOfFuncs().toString();
orgNumField.setText(orgStr);
funcNumField.setText(funcStr);
String extendOderKom = exKomRadio.getSelection()
.getActionCommand();// which Radio
FormEvent ev = new FormEvent(this, orgStr, funcStr,
extendOderKom);
drawBtn.setEnabled(false);
resetBtn.setEnabled(true);
if (formListener != null) {
formListener.formEventOccured(ev);
}
}
}
});
但是当我单击formPanel中的Draw按钮时,我看到了这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at imise.IT_Saturation_Matrix.view.FormPanel$1.actionPerformed(FormPanel.java:129)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:740)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:699)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:713)
at java.awt.EventQueue$4.run(EventQueue.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:710)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
路径设置正确,我不知道为什么这不起作用! 如果我传递了parsFile.parsingData(path);方法直接从Model到formPanel,而不是从controllPanel调用它,但是我希望保持我的视图和模型分离。