尝试向数据库显示数据时的JavaFX NullPointer

时间:2016-05-07 20:20:47

标签: mysql database button javafx

我正忙着学习JavaFX的小项目。 当我做一个特定的动作时,我得到一个NullPointer,我无法弄清楚为什么会出现这个错误。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at org.hva.klm.projectagiledevelopment.MedewerkerMainController.databaseVrachtShow(MedewerkerMainController.java:308)
at org.hva.klm.projectagiledevelopment.MedewerkerMainController$1.handle(MedewerkerMainController.java:272)
at org.hva.klm.projectagiledevelopment.MedewerkerMainController$1.handle(MedewerkerMainController.java:260)
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.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.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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

这些是发生错误的Controller类中的方法:

@FXML
public void zoekVracht() {
    actionError.setText(" ");
    try {
        // Check if field is empty
        if (ZoekVrachtInvoer.getText().equals("")) {
            actionError.setFill(Color.FIREBRICK);
            actionError.setText("Geen invoer... Probeer nogmaals.");
        } else {

            //database connection for first user
            Statement statement = dbCon.conDatabase().createStatement();
            //getting count for buttonList size
            ResultSet resultCount = statement.executeQuery("SELECT COUNT(*)"
                    + " from reservatie"
                    + " where idReservatie like '%" + ZoekVrachtInvoer.getText() + "%'");

            resultCount.next();
            numberOfRows = resultCount.getInt(1);

            // Select all reservations which meet the textfield characters
            ResultSet rs = statement.executeQuery("SELECT * from reservatie "
                    + "where idReservatie like '%"
                    + ZoekVrachtInvoer.getText() + "%';"
            );
            // Check if any reservations exsists
            if (!rs.next()) {
                actionError.setFill(Color.FIREBRICK);
                actionError.setText("Barcode niet gevonden");
                // List all found barcodes
            } else {
                i = 0;
                rs.beforeFirst();
                buttonList = new Button[numberOfRows];
                while (rs.next()) {
                    final int account_id = rs.getInt("idReservatie");

                    String username = rs.getString("Naam");
                    String firstname = rs.getString("gewicht");
                    String lastname = rs.getString("Lengte");
                    //  int status = rs.getInt("status");
                    gpTable.add(new Label(username), 0, i);
                    gpTable.add(new Label(firstname), 1, i);
                    gpTable.add(new Label(lastname), 2, i);

                    buttonList[i] = new Button("Edit reservatie");
                    gpTable.add(buttonList[i], 4, i);
                    buttonList[i].setOnAction(new EventHandler<ActionEvent>() {

                        @Override
                        public void handle(ActionEvent event) {
                            try {

                                Parent home_page_parent = FXMLLoader.load(FXMLDocumentController.class.getResource("FXMLInvoerVracht2.fxml"));
                                Scene home_page_scene = new Scene(home_page_parent);
                                Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                                app_stage.hide();
                                app_stage.setScene(home_page_scene);
                                app_stage.show();
                                databaseVrachtShow(account_id);
                                System.out.println(account_id);
                            } catch (IOException ex) {
                                Logger.getLogger(MedewerkerMainController.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });

                    i++;
                }
            }
        }

    } catch (SQLException ex) {
        Logger.getLogger(MedewerkerMainController.class.getName())
                .log(Level.SEVERE, null, ex);
    } finally {
        try {
            dbCon.conDatabase().close();
        } catch (Exception ex) {
            Logger.getLogger(MedewerkerMainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

@FXML
public void databaseVrachtShow(int account_id) {
    System.out.println(account_id);
    try {
        Statement statement = dbCon.conDatabase().createStatement();

        ResultSet resultSet = statement.executeQuery("SELECT * from reservatie "
                + "where idReservatie = " + account_id + "");

        while (resultSet.next()) {
            //    NaamDatabase.setText(resultSet.getString("Naam"));
            GewichtDatabase.setText(resultSet.getString("Gewicht"));
            LengteDatabase.setText(resultSet.getString("Lengte"));
            BreedteDatabase.setText(resultSet.getString("Breedte"));
            HoogteDatabase.setText(resultSet.getString("Hoogte"));
            //DatumDatabase.setText(resultSet.getString("Nog open"));

        }
    } catch (SQLException ex) {
        Logger.getLogger(MedewerkerMainController.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            dbCon.conDatabase().close();
        } catch (Exception ex) {
            Logger.getLogger(MedewerkerMainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

此行发生错误:GewichtDatabase.setText(resultSet.getString(“Gewicht”)); 不知何故,标签不能设置为getString应该获取的值。 所有标签都有自己的ID等。

根据要求:“

    @FXML
private Label GewichtDatabase;
@FXML
private Label LengteDatabase;
@FXML
private Label BreedteDatabase;
@FXML
private Label HoogteDatabase;

和fxml文件代码:

      <Label fx:id="GewichtDatabase" layoutX="526.0" layoutY="106.0" />
  <Label fx:id="LengteDatabase" layoutX="526.0" layoutY="134.0" />
  <Label fx:id="BreedteDatabase" layoutX="526.0" layoutY="164.0" />
  <Label fx:id="HoogteDatabase" layoutX="526.0" layoutY="195.0" />

有人能帮助我吗?

0 个答案:

没有答案