当我在服务器添加客户端时尝试更新我的 JavaFX 应用程序时,当我使用我的更新功能时,我会收到我的 FXML 列表视图的空指针。任何线索为什么它会在我的初始化和事件中起作用,但在我的更新方法中不起作用
FXML控制器
public class FXMLController implements Initializable, Remote, ClientObserver {
@FXML private ListView listview;
List<RMIClient> clients;
RMIServer server;
public FXMLController(RMIServer server) throws RemoteException
{
this.server = server;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
if(server != null) {
try {
ObservableList<RMIClient> clients = FXCollections.observableArrayList();
clients.addAll(server.getClients());
if(listview.getItems() == null)
this.listview.setItems(clients);
else
this.listview.getItems().setAll(clients);
} catch (RemoteException ex) { }
}
}
@Override
public void update() {
System.out.println(listview);
}
}
我的主应用
public class MainApp extends Application {
public static RMIServer server;
private FXMLController controller;
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/Scene.fxml"));
controller = new FXMLController(server);
loader.setController(controller);
server.setObserver(controller);
Parent root = loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add("/styles/Styles.css");
stage.setTitle("RMINetwork Centre");
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
server = (RMIServer) Naming.lookup("rmi://localhost:1099/" + RMIServer.class.getTypeName());
}
catch(Exception e) { }
launch(args);
}
}
场景.fxml
<BorderPane prefHeight="350" prefWidth="500" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
<left>
<ListView fx:id="listview" prefHeight="398.0" prefWidth="152.0" onMouseClicked="#ChangeContext" />
</left>
</BorderPane>
服务器类:
public class Server extends UnicastRemoteObject implements RMIServer {
private List<RMIClient> clients;
private ClientObserver observer;
public Server() throws RemoteException
{
this.clients = new ArrayList<>();
}
@Override
public void registerClient(RMIClient client) throws RemoteException
{
this.clients.add(client);
if(observer != null)
{
this.observer.update();
}
}
@Override
public void removeClient(RMIClient client) throws RemoteException
{
this.clients.remove(client);
if(observer != null)
{
this.observer.update();
}
}
@Override
public String notifyClient(int index, RMICommand cmd) throws RemoteException
{
return this.clients.get(index).executeCommand(cmd);
}
@Override
public List<RMIClient> getClients() throws RemoteException
{
return this.clients;
}
@Override
public void setObserver(ClientObserver observer) throws RemoteException {
this.observer = observer;
}
那么有没有人可以解决我的问题?
亲切的问候