Ilya Kulikov Ilya Kulikov - 15 days ago 4
Java Question

Can't fix NullPointerException when transfering values through views

i need some help. I want to transfer value from SaleControlle idOf5match, to BuyController idText(label). But i'm getting


NullPointerException


I tryed different method which i read in another answers in stuck overflow, but they don't working. Please tell me what i did wrong

Debbuger screen

Sale Controller Class

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

Answer

i guess we will need more information about this issue but can you try debugging this line 'idOf5match= String.valueOf(matchData.get(4).getId_match())'. And find out if Dao is returning value or not. If not we have to check the Dao. If you find issue in Dao and cannot fix it then send us its code as well.