FX:ID ="网格"在控制器

时间:2018-03-27 12:30:14

标签: javafx nullpointerexception fxml gridpane

当用户点击调用' pressLookUp'的按钮时,

网格保持为空用户假设从旧阶段转到fxml文件中的新阶段。在显示新窗口之前,我想使用grid.add(node,colIndex,rowIndex)向数据库中的网格子项添加值

控制器:

package login;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import userGroups.User;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class studentOptionsController{
@FXML
private GridPane grid;//gridpane from ClassesToAdd

public void pressLookUp(ActionEvent event) throws IOException{
    Parent userOption = FXMLLoader.load(getClass().getResource("ClassesToAdd.fxml"));
    Scene classesToAddScene = new Scene(userOption);
    Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    app_stage.setScene(classesToAddScene);
    app_stage.show();

    Connection c = null;
    Statement stmt = null;
    try{
        c = DriverManager.getConnection("jdbc:sqlite:users.db");
        c.setAutoCommit(false);

        System.out.println("Opened database successfully");
        stmt = c.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM Classes WHERE IDCLASSES= " + "5;");

        int currentIndex = 0;
        int currentRow = 0;
        //assusming all columns are NOT NULL
        while(rs.next()){
            Label currentLabel = new Label();
            currentLabel.setText(rs.getString("CLASSNAME"));
            System.out.println(currentLabel.getText());
            grid.add(currentLabel, 5, 0 );
            /*if (rs.getString("SUSERNAME") != null && rs.getString("STUDENTPASS") != null){
                String  username = rs.getString("SUSERNAME");
                System.out.println( "SUSERNAME = " + username );
                String password = rs.getString("STUDENTPASS");
                System.out.println("STUDENTPASS = " + password);
            }*/
        }
        rs.close();
        stmt.close();
        c.close();
    } catch ( Exception e ){
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        //System.exit(0);
    }
    System.out.println("Operation done successfully");
}
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane fx:id="grid" prefHeight="400.0" prefWidth="850" 
xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="login.studentOptionsController">
  <columnConstraints>
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
      <ColumnConstraints percentWidth="11" />
       <ColumnConstraints percentWidth="12" /> <!-- This column for 'Add 
 Class' buttons ?-->
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Class Name" GridPane.columnIndex="0" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Size of Class" GridPane.columnIndex="1" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Spots Taken" GridPane.columnIndex="2" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Instructor" GridPane.columnIndex="3" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="CRN" GridPane.columnIndex="4" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Days" GridPane.columnIndex="5" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Time" GridPane.columnIndex="6" GridPane.rowIndex="0" />
  <Label maxHeight="Infinity" maxWidth="Infinity" style=" -fx-border-color:gray; " text="Subject" GridPane.columnIndex="7" GridPane.rowIndex="0" />
  <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#pressBack" style=" -fx-background-color:red; -fx-border-color:black" text="Back..." GridPane.columnIndex="8" GridPane.rowIndex="0" />
   </children>
</GridPane>

系统输出:

Opened database successfully
java.lang.NullPointerException: null
Operation done successfully

1 个答案:

答案 0 :(得分:0)

不知道为什么会这样,但确实如此。

public void pressLookUp(ActionEvent event) throws IOException{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("ClassesToAdd.fxml"));
    Parent userOption = loader.load();
    grid = loader.getRoot();
    Scene classesToAddScene = new Scene(userOption);
    Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    app_stage.setScene(classesToAddScene);
    app_stage.show();
    Connection c = null;
    Statement stmt = null;
    try{
        c = DriverManager.getConnection("jdbc:sqlite:users.db");
        c.setAutoCommit(false);

        System.out.println("Opened database successfully");
        stmt = c.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM Classes WHERE IDCLASSES= " + "5;");

        int currentIndex = 0;
        int currentRow = 0;
        //assusming all columns are NOT NULL
        while(rs.next()){
            Label currentLabel = new Label();
            currentLabel.setText(rs.getString("CLASSNAME"));
            System.out.println(currentLabel.getText());
            grid.add(currentLabel, 0, 1 );
        }
        loader.setRoot(grid);
        rs.close();
        stmt.close();
        c.close();
    } catch ( Exception e ){
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        System.exit(0);
    }
    System.out.println("Operation done successfully");
}