TableView刷新时JavaFX java.lang.NullPointerException

时间:2017-08-15 10:44:48

标签: javafx

我在添加新行后尝试刷新我的表但是我在行

中得到了这个错误
codeCol.setCellValueFactory(new PropertyValueFactory<>("userId")); 

当我从另一个控制器调用方法loadUserData()时。

第一次它工作正常..只有当我刷新表时。 你能帮忙吗......

public class UserTableController implements Initializable{

    @FXML
    private TableView<UserClass> tableView;
    @FXML
    private TableColumn<UserClass, String> codeCol;
    @FXML
    private TableColumn<UserClass, String> firstnameCol;
    @FXML
    private TableColumn<UserClass, String> lastnameCol;
    @FXML
    private TableColumn<UserClass, String> companyCol;
    @FXML
    private TableColumn<UserClass, String> usernameCol;
    @FXML
    private TableColumn<UserClass, String> emailCol;
    @FXML
    private TableColumn<UserClass, String> mobileCol;
    @FXML
    private TableColumn<UserClass, String> landlineCol;
    @FXML
    private TableColumn<UserClass, Boolean> adminCal;

    public ObservableList<UserClass> list;
    private  DatabaseHandler handler;
    private  Main mainApp;
    private MainController mainCont;
    private Stage stage;

    @FXML
    public  HBox bottomPane;
    @FXML
    private JFXButton addUserButton;


    @FXML
    public void initialize(URL url, ResourceBundle rb) {

        loadUserData();
    }
     public void setMainApp(Main mainApp) {
         //this.mainApp = mainApp;

        // pass the main app to the drawerContentController:
        mainCont.setMainApp(mainApp);
    }

     public void loadUserData() {

        handler = DatabaseHandler.getInstance();
        list = FXCollections.observableArrayList();
        list.clear();

        String qu = "select * from Users";
        ResultSet rs = handler.execQuery(qu);
         int counter = 0;
        try {
            while (rs.next()) {
                String nuserId = String.valueOf(rs.getInt("userId"));
                String nfirstName = rs.getString("firstname");
                String nlastName = rs.getString("lastName");
                String ncompanyName = rs.getString("companyName");
                String nuserName = rs.getString("userName");
                String nemail = rs.getString("email");
                String nmobile = rs.getString("mobile");
                String nlandline = rs.getString("landline");
                Boolean nadmin = rs.getBoolean("isadmin");
                counter = counter + 1;
                list.add(new UserClass(nuserId, nfirstName, nlastName, ncompanyName, nuserName, nemail, nmobile, nlandline, nadmin));
            }

        } catch (SQLException e) {

            System.out.println("Error loadData : " + e);

        }
        codeCol.setCellValueFactory(new PropertyValueFactory<>("userId"));
        System.out.println("test loadUserData 1");
        firstnameCol.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
        lastnameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        companyCol.setCellValueFactory(new PropertyValueFactory<>("companyName"));
        usernameCol.setCellValueFactory(new PropertyValueFactory<>("userName"));
        emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));
        mobileCol.setCellValueFactory(new PropertyValueFactory<>("mobile"));
        landlineCol.setCellValueFactory(new PropertyValueFactory<>("LandLine"));
      //  adminCal.setCellValueFactory(new PropertyValueFactory<>("IsAdmin"));
     // adminCal.setCellFactory(new );

     // ==== SINGLE? (CHECH BOX) ===
        adminCal.setCellValueFactory((CellDataFeatures<UserClass, Boolean> param) -> {
            UserClass userClass = param.getValue();

            SimpleBooleanProperty booleanProp = new SimpleBooleanProperty(userClass.getIsAdmin());

            // Note: singleCol.setOnEditCommit(): Not work for
            // CheckBoxTableCell.

            // When "Single?" column change.
            booleanProp.addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                userClass.setIsAdmin(newValue);
            });
            return booleanProp;
        });

        adminCal.setCellFactory((TableColumn<UserClass, Boolean> p) -> {
            CheckBoxTableCell<UserClass, Boolean> cell = new CheckBoxTableCell<UserClass, Boolean>();
            cell.setAlignment(Pos.CENTER);
            return cell;
        } //
        );

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        tableView.setItems(null);
        tableView.setItems(list);

    }
    @FXML
    private void addUserButtonAction(ActionEvent event) throws IOException {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/Users/UserDialog.fxml"));
        BorderPane addNewUser = loader.load();
        //mainLayout.setCenter(mainItems);

        Stage addDialogStage = new Stage();
        addDialogStage.setTitle("Add New User");
        addDialogStage.initModality(Modality.WINDOW_MODAL);// windows model make my new stage allows on top
        addDialogStage.initOwner(stage);
        addDialogStage.initStyle(StageStyle.UNDECORATED);
        Scene scene = new Scene(addNewUser);
        addDialogStage.setScene(scene);
        addDialogStage.showAndWait();

    }
}

从另一个控制器我调用此loadData来刷新我的tableview但是出现了这个错误

堆栈追踪:

java.lang.NullPointerException 
at Users.UserTableController.loadUserData(UserTableController.j‌​ava:128) 
at Users.UserDialogController.saveUser(UserDialogController.jav‌​a:146) 
at Users.UserDialogController.saveButtonAction(UserDialogContro‌​ller.java:85) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce‌​ssorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke

/ ------------------

public class UserDialogController implements Initializable {

private static Main mainApp;
DatabaseHandler databaseHandler;
private MainController mainCont;
private UserTableController userTableController;


@FXML
private BorderPane borderPane;
@FXML
private Button closeButton;
@FXML
private JFXTextField txtfirstname;
@FXML
private JFXTextField txtlastname;
@FXML
private JFXTextField txtcompany;
@FXML
private JFXTextField txtusername;
@FXML
private JFXPasswordField txtpassword;
@FXML
private JFXTextField txtemail;
@FXML
private JFXTextField txtmobile;
@FXML
private JFXTextField txtlandline;
@FXML
private CheckBox cbxadmin;

 public UserDialogController() {

    databaseHandler = DatabaseHandler.getInstance();
   // mainController = new MainController();
     userTableController =  new UserTableController();
    mainApp = new Main();

}
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

@FXML
private void saveButtonAction(ActionEvent event) {
    saveUser();
}

@FXML
private void closeImageViewAction(MouseEvent event) {
    closeAction();
}

@FXML
private void closeButtonAction(ActionEvent event) {
   closeAction();
}
void closeAction() {
    Stage stage = (Stage) closeButton.getScene().getWindow();
    stage.close();
}
void saveUser(){
   //  System.out.println("USERNAME In User Dialog : " + mainApp.getUserNAME());
//    System.out.println(" My Action  :" + mainApp.getMyAction());
    String xFirstFame = txtfirstname.getText();
    String xLastName = txtlastname.getText();
    String xCompany = txtcompany.getText();
    String xUsername = txtusername.getText();
    String xPassword = txtpassword.getText();
    String xEmail = txtemail.getText();
    String xMobile = txtmobile.getText();
    String xLandLine = txtlandline.getText();
    int xAdmin = 0;
    if (cbxadmin.isSelected()) {
        xAdmin = 1;
    }

    String st = "INSERT INTO Users (FirstName,LastName,CompanyName,UserName,Password,Email,Mobile,LandLine,isAdmin ) VALUES ("
            + "'" + xFirstFame + "',"
            + "'" + xLastName + "',"
            + "'" + xCompany + "',"
            + "'" + xUsername + "',"
            + "'" + xPassword + "',"
            + "'" + xEmail + "',"
            + "'" + xMobile + "',"
            + "'" + xLandLine + "',"
            + "'" + xAdmin + "'"
            + " )";
    if (databaseHandler.execAction(st)) {
        try {

            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setHeaderText(null);
            alert.setContentText("Success ..Saved");
            alert.showAndWait();
            System.out.println("test 1");

           userTableController.loadUserData();

       //       mainController.tableView.refresh();
            closeAction();
        } catch (Exception e) {
            System.out.println("Error in add User : " + e);
            e.printStackTrace();
        }
    } else {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Failed ....");
        alert.showAndWait();
    }

}

}

0 个答案:

没有答案