When the button is clicked, the Stage should open with a table in which there is a TableView. I implement it in this way, I get the fxml Scene, add data to the TableView table of this scene, send the Stage.
Implementation: Main:
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; import java.io.IOException; import java.util.ArrayList; import static Client.NET.Client.STOP; import static Client.NET.Client.ShowCar; import static Client.NET.Client.getInstanceClient; public class Main extends Application { public static Stage primaryStage; @Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("/Client/FXML/Scene1.fxml")); primaryStage.setScene(new Scene(root)); runStage(primaryStage); } public static void setStage(Stage stage) { primaryStage = stage; } public static Stage getStage() {return primaryStage;} public static void setScene(Scene scene) { primaryStage.setScene(scene); } public static Scene getScene() { return primaryStage.getScene(); } public static void runStage(Stage stage) throws IOException { primaryStage = stage; primaryStage.setTitle("ClientProgram"); primaryStage.setMinHeight(300); primaryStage.setMinWidth(530); primaryStage.setMaxHeight(400); primaryStage.setMaxWidth(630); getInstanceClient(); primaryStage.show(); } public static void main(String[] args) { launch(args); } } Controller1:
public void IsAuto(ActionEvent actionEvent) throws IOException { Parent root = FXMLLoader.load(getClass().getResource("/Client/FXML/ShowAuto.fxml")); Main.primaryStage.setScene(new Scene(root)); ObservableList<TableCar> Cars = FXCollections.observableArrayList(Inish()); Marka.setCellValueFactory(new PropertyValueFactory<TableCar,String>("Markat")); Model.setCellValueFactory(new PropertyValueFactory<TableCar,String>("Modelt")); Color.setCellValueFactory(new PropertyValueFactory<TableCar,String>("Colort")); Color.setCellValueFactory(new PropertyValueFactory<TableCar,String>("Modelt")); Price.setCellValueFactory(new PropertyValueFactory<TableCar,Float>("Pricett")); Count.setCellValueFactory(new PropertyValueFactory<TableCar,Integer>("Countt")); TableCars.setItems(Cars); Main.primaryStage.show(); } Errors occur:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Node.fireEvent(Node.java:8411) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) ... 48 more Caused by: java.lang.NullPointerException at Client.CONTROLLER.Controller1.IsAuto(Controller1.java:116) ... 58 more There is a change of Stage, but the table is empty. I think that the problem is in the correctness of the interaction of adding data to the table and changing the Stage.
TableCar class:
public class TableCar { private String markat; private String modelt; private String colort; private String yeart; private float pricet; private int countt; public String getMarkat() { return markat; } public void setMarkat(String markat) { this.markat = markat; } public String getModelt() { return modelt; } public void setModelt(String modelt) { this.modelt = modelt; } public String getColort() { return colort; } public void setColort(String colort) { this.colort = colort; } public String getYeart() { return yeart; } public void setYeart(String yeart) { this.yeart = yeart; } public float getPricet() { return pricet; } public void setPricet(float pricet) { this.pricet = pricet; } public int getCountt() { return countt; } public void setCountt(int countt) { this.countt = countt; } }
Inish () method:
public ArrayList Inish() { ArrayList<Goods> arrayList = ShowCar();//ShowCar - возвращает ArrayList c данными ArrayList<TableCar> tableCars = new ArrayList<>(); for(int i = 0; i<arrayList.size();i++) { TableCar tableCar = new TableCar(); tableCar.setMarkat(arrayList.get(i).getCar().getMarka()); tableCar.setModelt(arrayList.get(i).getCar().getModel()); tableCar.setColort(arrayList.get(i).getCar().getColor()); tableCar.setYeart(arrayList.get(i).getCar().getYear()); tableCar.setPricet(arrayList.get(i).getPrice()); tableCar.setCountt(arrayList.get(i).getCount()); tableCars.add(tableCar); } System.out.println("Проверка:" + tableCars.size()); return tableCars; }