通过视图传输值时无法修复NullPointerException

时间:2016-11-22 03:26:25

标签: java javafx nullpointerexception

我需要一些帮助。我想将值从SaleControlle idOf5match转移到BuyController idText(label)。但是我得到了

  

的NullPointerException

我尝试了不同的方法,我在其他答案中读到了卡住的溢出,但他们没有工作。请告诉我我做错了什么

Debbuger screen

销售控制器类

package sample.controller;

public class SaleController {
    @FXML
    private Button button1;

    private Stage stage;
    public String idOf1match;
    public String idOf2match;
    public String idOf3match;
    public String idOf4match;
    public String idOf5match;

    @FXML
    private void initialize () throws SQLException, ClassNotFoundException {
        initLabels();
    }

    private void initLabels() throws SQLException, ClassNotFoundException {
        ObservableList<Match> matchData = MatchDAO.searchAllMatches();
        idOf1match= String.valueOf(matchData.get(0).getId_match());
        idOf2match= String.valueOf(matchData.get(1).getId_match());
        idOf3match= String.valueOf(matchData.get(2).getId_match());
        idOf4match= String.valueOf(matchData.get(3).getId_match());
        idOf5match= String.valueOf(matchData.get(4).getId_match());
        dataText1.setText(matchData.get(0).getDate());
        dataText2.setText(matchData.get(1).getDate());
        dataText3.setText(matchData.get(2).getDate());
        dataText4.setText(matchData.get(3).getDate());
        dataText5.setText(matchData.get(4).getDate());
        teamText1.setText(matchData.get(0).getTeam());
        teamText2.setText(matchData.get(1).getTeam());
        teamText3.setText(matchData.get(2).getTeam());
        teamText4.setText(matchData.get(3).getTeam());
        teamText5.setText(matchData.get(4).getTeam());
        enemyText1.setText(matchData.get(0).getEnemy());
        enemyText2.setText(matchData.get(1).getEnemy());
        enemyText3.setText(matchData.get(2).getEnemy());
        enemyText4.setText(matchData.get(3).getEnemy());
        enemyText5.setText(matchData.get(4).getEnemy());
    }

    @FXML
    private void buyTicket1() throws IOException {
        BuyController buyController = new BuyController();
        lastFormShow();
    }

    @FXML
    private void buyTicket2() throws IOException {
        BuyController buyController = new BuyController();
        lastFormShow();   
    }

    @FXML
    private void buyTicket3() throws IOException {
        BuyController buyController = new BuyController();
        lastFormShow();
    }

    @FXML
    private void buyTicket4() throws IOException {
        BuyController buyController = new BuyController();
        lastFormShow();
    }

    @FXML
    private void buyTicket5() throws IOException {
        BuyController buyController = new BuyController();
        stage = (Stage) button1.getScene().getWindow();

        buyController.redirectHome(stage, idOf5match);
        buyController.idText.setText(idOf5match);
        // lastFormShow();
    }

    private void lastFormShow() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getClassLoader().getResource("sample/UI/BuyTicketLayout.fxml"));

        AnchorPane content = (AnchorPane) loader.load();
        Stage stage = new Stage();
        stage.setTitle("shit");
        stage.setScene(new Scene(content));
        stage.show();
    }
}

BuyController Class

public class BuyController {
    private Parent parent;
    private Scene scene;
    private Stage stage;
    public String enemy;
    public String team;
    public String id;

    @FXML
    private void searchFreePlaces(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
        try {
            //Get all Event information
            ObservableList<String> areaData =     AreaDAO.searchAllPlaces(idText.getText(),areaText.getSelectionModel().getSelectedItem(),"free");
            //Populate Employees on TableView
            freeplaceCombo.setItems(areaData);
        } catch (SQLException e){
            System.out.println("Error occurred while getting team information from DB.\n" + e);
            throw e;
        }
    }

    @FXML
    private void buyTicket(){

    }

    public BuyController() {
        FXMLLoader fxmlLoader = new     FXMLLoader(getClass().getClassLoader().getResource("sample/UI/BuyTicketLayout.fxml"));
        fxmlLoader.setController(this);
        try {
            parent = (Parent) fxmlLoader.load();
            scene = new Scene(parent, 600, 400);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void redirectHome(Stage stage, String name) {
        stage.setTitle("Home");
        stage.setScene(scene);
        // idText.setText(name);
        stage.hide();
        stage.show();
    }
}

1 个答案:

答案 0 :(得分:0)

我想我们需要有关此问题的更多信息,但您可以尝试调试此行&#39; idOf5match = String.valueOf(matchData.get(4).getId_match())&#39;。并找出Dao是否返回价值。如果不是,我们必须检查道。如果您在Dao中发现问题并且无法解决问题,请将代码发送给我们。