【问题标题】:Can't fix NullPointerException when transfering values through views通过视图传输值时无法修复 NullPointerException
【发布时间】:2016-11-22 03:26:25
【问题描述】:

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

空指针异常

我尝试了不同的方法,我在卡住溢出的另一个答案中读到了这些方法,但它们不起作用。请告诉我我做错了什么

销售控制类

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 类

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();
    }
}

【问题讨论】:

  • 我有时做的是使用属性。当我关闭一个视图时,我设置了属性。当我打开下一个视图时,我会得到这些属性。当我试图从一个视图到另一个视图中获取少量数据时,我会使用此技术。

标签: java javafx nullpointerexception


【解决方案1】:

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

【讨论】:

  • 我在 idOf5match 中有一个值,但我不能将它分配给 idText,我添加了屏幕
  • 我在 idOf5match 中有一个值,但我不能将它分配给 idText,我添加了屏幕
  • 可能是您发布的内容未更新,但我在 BuyController 中没有看到 idText 字段。您能确认一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多