我有控制器,我尝试从电子邮件服务器获取 INBOX 文件夹。下载一切正常。我可以将这些数据(电子邮件主题、发件人、日期)放入 TableView 但......只有当我等待负责在 TableView 中设置这些数据的线程时。代码:
// The table and columns
@FXML
TableView<MainModel.Item> itemTbl;
@FXML
TableColumn itemNameCol;
@FXML
TableColumn itemQtyCol;
@FXML
TableColumn itemTitleCol;
// The table's data
static ObservableList<MainModel.Item> data;
public void dataSet(){
// Set up the table data
itemNameCol.setCellValueFactory(
new PropertyValueFactory<MainModel.Item,String>("name")
);
itemQtyCol.setCellValueFactory(
new PropertyValueFactory<MainModel.Item,String>("qty")
);
itemTitleCol.setCellValueFactory(
new PropertyValueFactory<MainModel.Item,String>("title")
);
data = FXCollections.observableArrayList();
itemTbl.setItems(data);
}
public void setEnemyEvent() {
itemTbl.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 2) {
if(itemTbl.getSelectionModel().getSelectedItem()!=null){
System.out.println(itemTbl.getSelectionModel().getSelectedItem().name);
}
}
}
});
}
@Override
public void initialize(URL url, ResourceBundle rb) {
setEnemyEvent();
dataSet();
Thread t = new Thread(new MessageLoop());
//HERE:
t.start();
//of course normally here is try and catch
t.join();
}
//staric nested class "for loop"
private static class MessageLoop implements Runnable {
public void run() {
try {
EmailConnetion con = new EmailConnetion();
MainModel MainModel = new MainModel();
for(int i=0;i<con.message.length;i++){
try{
MainModel.Item item = MainModel.new Item();
item.name.setValue(i+""+con.message[i].getFrom()[0].toString());
item.qty.setValue(con.message[i].getSentDate().toString());
item.title.setValue(con.message[i].getSubject().toString());
//Here is a problem.
data.add(item);//This indicate my IDE
//*****************
} catch(AddressException e) {
System.out.println("Error : " + "AddressException:"+i);
}
}
} catch (NoSuchProviderException ex) {
Logger.getLogger(SimpleController.class.getName()).log(Level.SEVERE, null, ex);
} catch (MessagingException ex) {
Logger.getLogger(SimpleController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
当我删除 t.join() 时,它会抛出这个 NullPointerException。
Exception in thread "Thread-3" java.lang.NullPointerException
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:291)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:74)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel$3.onChanged(TableView.java:1725)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:134)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
at com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:184)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.quietClearSelection(TableView.java:2154)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.updateSelection(TableView.java:1902)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.access$2600(TableView.java:1681)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel$8.onChanged(TableView.java:1802)
at com.sun.javafx.scene.control.WeakListChangeListener.onChanged(WeakListChangeListener.java:71)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:291)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:154)
at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:144)
at javasplitpane.SimpleController$MessageLoop.run(SimpleController.java:95)
at java.lang.Thread.run(Thread.java:722)
这出现在不同的位置,在十四封电子邮件,三十封电子邮件中。你有什么想法吗?
ps 我等不及下载了,因为它需要 20 秒。有 50 封电子邮件 - 我必须在初始化期间执行此操作。