I am creating a game that includes a weapon shop(In a different scene), in the weapon shop are buttons which execute methods that create and place the images in the respective flowpane. but when running an error occurs which says “this.lane1Wall” which is the flowpane is null. I have no idea how to fix this error. Any Ideas?
PS: I tried putting a button in the same scene as the battle(The same scene as the flowpane) and it works??? but not when the button is in the weapon shop.
Code:
package game.gui;
import game.engine.Battle;
import game.engine.BattlePhase;
import game.engine.base.Wall;
import game.engine.lanes.Lane;
import game.gui.*;
import game.engine.titans.Titan;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.PriorityQueue;
public class EasyBattleController {
private Stage stage= new Stage();
private Scene scene ;
private Parent root;
private Battle easygamebattle;
{
try {
easygamebattle = new Battle(1, 0, 100, 3 , 250);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Lane lane1 = easygamebattle.getOriginalLanes().get(0);
private Lane lane2 = easygamebattle.getOriginalLanes().get(1);
private Lane lane3 = easygamebattle.getOriginalLanes().get(2);
private Wall wall1 = lane1.getLaneWall();
private Wall wall2 = lane2.getLaneWall();
private Wall wall3 = lane3.getLaneWall();
@FXML
private Button PassTurnButton;
@FXML
private Label ScoreLabel = new Label();
@FXML
private Label TurnLabel= new Label();
@FXML
private Label PhaseLabel= new Label();
@FXML
private Label Lane1Health= new Label();
@FXML
private Label Lane2Health= new Label();
@FXML
private Label Lane3Health= new Label();
@FXML
private Button WeaponShopButton;
@FXML
private Label ResLabel= new Label();
@FXML
private Label Lane1Danger= new Label();
@FXML
private Label Lane2Danger= new Label();
@FXML
private Label Lane3Danger= new Label();
@FXML
private Button BackToGameButton = new Button();
@FXML
private FlowPane lane1Wall;
@FXML
private FlowPane lane2Wall;
@FXML
private FlowPane lane3Wall;
@FXML
private Button dddd;
@FXML
private Label ResLabelShop =new Label();
@FXML
private Button PiercingButLane1= new Button();
public void passTurnPress (){
easygamebattle.passTurn();
ScoreLabel.setText("Score:" + easygamebattle.getScore());
TurnLabel.setText("Turn:" + easygamebattle.getNumberOfTurns());
PhaseLabel.setText(String.valueOf(easygamebattle.getBattlePhase()));
ResLabel.setText((String.valueOf(easygamebattle.getResourcesGathered())));
Lane1Health.setText(String.valueOf(wall1.getCurrentHealth()));
Lane2Health.setText(String.valueOf(wall2.getCurrentHealth()));
Lane3Health.setText(String.valueOf(wall3.getCurrentHealth()));
Lane1Danger.setText(String.valueOf(lane1.getDangerLevel()));
Lane2Danger.setText(String.valueOf(lane2.getDangerLevel()));
Lane3Danger.setText(String.valueOf(lane3.getDangerLevel()));
}
public void switchToWeaponShop (ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("weaponshop.fxml"));
Stage stageweaponshop = new Stage();
Scene sceneweaponshop = new Scene(root);
stageweaponshop.setScene(sceneweaponshop);
stageweaponshop.setResizable(true);
stageweaponshop.setHeight(780);
stageweaponshop.setWidth(1280);
stageweaponshop.show();
ResLabelShop.setText((String.valueOf(easygamebattle.getResourcesGathered())));
}
public void piercingDeployLane1() {
Image piercing = new Image( getClass().getResourceAsStream("Piercingcannon.png"));
ImageView imageView = new ImageView(piercing);
imageView.setFitHeight(50);
imageView.setFitWidth(50);
lane1Wall.getChildren().add(imageView);
}
I tried putting a button in the same scene as the battle(The same scene as the flowpane) and it works??? but not when the button is in the weapon shop.
Ibrahim Darwish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.