我正在做一个项目,我需要为MySQL数据库的每一行向HBox添加一个带有3个ComboBox的HBox。

我已经尝试过一些方法,但是总是出错。所以我尝试了诸如动态变量名之类的方法。

@FXML
    private VBox mon_routes;

public void initialize(URL url, ResourceBundle rb) {
        PreparedStatement pr;
        ResultSet rs;
        String sql = "SELECT * FROM routes";

        try {
            pr = this.connection.prepareStatement(sql);
            rs = pr.executeQuery();
            int n = 1;
            HBox r[] = new HBox[n];
            while (rs.next()) {
                r[n].getChildren().add(new CheckBox());
                r[n].getChildren().add(new ComboBox());
                r[n].getChildren().add(new ComboBox());
                mon_routes.getChildren().add(r[n]);
                n++;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }


我希望获得带有7个HBox的VBox,因为我的数据库有7行。
但是我只是犯了很多错误。

javafx.fxml.LoadException:
unknown path

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
    at loginapp.LoginController.adminLogin(LoginController.java:110)
    at loginapp.LoginController.login(LoginController.java:92)
    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)
    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:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    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$152(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at adminapp.AdminController.initialize(AdminController.java:56)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 61 more

Process finished with exit code 0

最佳答案

除非必须,否则应避免使用Arrays。我会使用ListHBoxes。您的代码应类似于以下代码。阅读代码中的注释。

VBox mon_routes = new VBox(); //You never create the VBox. This leads to a null pointer exception.

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    PreparedStatement pr;
    ResultSet rs;
    String sql = "SELECT * FROM routes";

    try {
        pr = this.connection.prepareStatement(sql);
        rs = pr.executeQuery();

        List<HBox> r = new ArrayList();//Avoid Arrays. Use List instead.

        while (rs.next()) {
            HBox tempHBox = new HBox();//Create an HBox to store into the List or Array if you continue to use the Array.
            tempHBox.getChildren().add(new CheckBox(rs.getString(0)));//You never retrieve anything from the result set. <- rs.getSomething(). Add the CheckBox to the HBox
            tempHBox.getChildren().add(new CheckBox(rs.getString(1)));//You never retrieve anything from the result set. <- rs.getSomething(). Add the CheckBox to the HBox
            tempHBox.getChildren().add(new CheckBox(rs.getString(2)));//You never retrieve anything from the result set. <- rs.getSomething(). Add the CheckBox to the HBox
            r.add(tempHBox);//Add the HBox to the List
        }

        mon_routes.getChildren().addAll(r);//Add the List of HBox to the VBox.
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}

关于java - JavaFX + SQL:为SQL数据库的每一行添加1个带有3个组合框的新HBox,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56484776/

10-12 13:24