问题
我的代码在ModelDraftContract.java
以下是按钮触发事件时出现的错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at control.TaskListListener.actionPerformed(TaskListListener.java:31)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是我的模型的代码。这是我从中获取值的地方。
ModelContractDraft.java
public class ModelDraftContract {
private String inputFile;
private String selectedTask;
public ModelDraftContract(){
// constructor
}
public void setSelectedTask(String selectedTask) {
this.selectedTask = selectedTask;
}
public String getSelectedTask() {
return selectedTask;
}
}
这是我的GUI,我使用SelectionList.java
ViewTaskList.java
public class ViewTaskList extends JFrame{
private JPanel mainPanel;
private JList<String> taskList;
private JLabel activityListLabel, taskLabel;
private final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
private JButton btnOk, btnCancel;
private JComboBox comboList;
public void createGUI(){
setSize(600, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
setLocationRelativeTo(null);
setResizable(false);
mainPanel = new JPanel(null);
mainPanel.setPreferredSize(new Dimension(600,300));
taskLabel = new JLabel("Choose a task");
taskLabel.setBounds(400, 30, 100, 100);
comboList = new JComboBox();
comboList.setBounds(400, 100, 150, 30);
comboList.setSelectedItem(null);
DefaultListModel<String> JListModelTask = new DefaultListModel<>();
JListModelTask.addElement("Create Draft Contract");
//listModelTask.addElement("WTAAS");
JListModelTask.addElement("Create VN Shell");
//listModelTask.addElement("Prepare MES Invoice");
activityListLabel = new JLabel("Activity");
taskList = new JList<>(JListModelTask);
taskList.addListSelectionListener(new SelectionList(comboList, taskList));
taskList.setBorder(new LineBorder(Color.BLACK));
activityListLabel.setBounds(50, 30, 100, 100);
taskList.setBounds(50, 100, 150, 100);
btnOk = new JButton("Ok");
btnOk.setActionCommand("Ok");
btnOk.addActionListener(new TaskListListener());
btnOk.setBounds(150, 250, 70, 30);
btnCancel = new JButton("Cancel");
btnCancel.setActionCommand("Cancel");
btnCancel.addActionListener(new TaskListListener());
btnCancel.setBounds(350, 250, 100, 30);
mainPanel.add(btnCancel);
mainPanel.add(btnOk);
mainPanel.add(taskLabel);
mainPanel.add(comboList);
mainPanel.add(activityListLabel);
mainPanel.add(taskList);
add(mainPanel);
revalidate();
pack();
}
public void initialize(){
createGUI();
}
}
此代码包含ViewTaskList.java
TaskListListener.java
public class TaskListListener implements ActionListener{
ModelDraftContract modelContract;
ViewTaskList tasklist;
public TaskListListener(ModelDraftContract modelContract){
this.modelContract = modelContract;
}
public TaskListListener(){
}
@Override
public void actionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
if(evt.getActionCommand().equals("Run Vinny")){
tasklist = new ViewTaskList();
tasklist.initialize();
tasklist.setVisible(true);
}
else if(evt.getActionCommand().equals("Ok")){
// THROWS NULL POINTER EXCEPTION
JOptionPane.showMessageDialog(null, modelContract.getSelectedTask());
}
else if(evt.getActionCommand().equals("Ok")){
//ControllerDraftContract conDraft = new ControllerDraftContract();
JOptionPane.showMessageDialog(null, "VN SHELL");
}
}
}
此代码实现ListSelectionListener
,其中我需要基于JComboBox中所选的值。
SelectionList.java
public class SelectionList implements ListSelectionListener{
private JComboBox comboList;
private JList<String> taskList;
private TaskListListener l;
public SelectionList(JComboBox comboList, JList<String> taskList){
this.taskList = taskList;
this.comboList = comboList;
}
public SelectionList() {
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
ModelDraftContract modelContract = new ModelDraftContract();
String selected = "";
if(taskList.getSelectedIndex() == 0){
comboList.removeAllItems();
comboList.addItem("Prepare Contract");
selected = comboList.getSelectedItem().toString();
}
else if(taskList.getSelectedIndex() == 1){
comboList.removeAllItems();
comboList.addItem("VN Shell(New)");
comboList.addItem("VN Shell(Old)");
selected = comboList.getSelectedItem().toString();
}
modelContract.setSelectedTask(selected);
}
}
我使用JOptionPane
进行测试,看看我是否正确获得了值。但我得到的只是一个错误。
我一直陷入这个问题,我不知道它有什么问题。有什么想法吗?