1

I'm making a fairly large text based game in java using java fx and fxml controllers to represent each location in the game.

I have a player class that represents the player stats table in my Database. The player needs to talk to the Jarl before he can do the quest in the northen woods.

So I use a level counter integer inside my player class which can be stored in databse when user saves game. After the jarl is spoken to, the player goes the gate which the gate guard determines weather the player is ready to do the quest. When I try click "go to northen woods". It eithers returns 0 or in this case I get a NullPointerException.

I know I just can't make a new instance of the player class in the gate controller because it would just default to zero, but how can I have a generic method in my player class that allows setters/getters to be used in multiple classes?

I try to set using the constructor, but that didn't work either. I checked my assumptions using system.out.println and it still returns 0.

player class.

public class Player {

int health = 100, attackdamage = 100, armor = 0, bank = 100, Game_ID = 0, Jarl_level_count = 0;

/**
 *
 * @param Name
 * @param Description
 * @param Cost
 * @param health
 */
public Player() {
    this.Game_ID = 0;
    this.Jarl_level_count = 0;
    this.armor = 0;

    this.attackdamage = 0;
    this.bank = 0;
    this.health = 0;
}

public Player(int Game_ID, int Jarl, int attackdamage, int armor, int bank, int health) {

    this.Game_ID = Game_ID;
    this.Jarl_level_count = Jarl;
    this.armor = armor;

    this.bank = bank;
    this.attackdamage = attackdamage;
    this.health = health;
}

public void update(Player thisThing) {
    health = thisThing.health;
    bank = thisThing.bank;
    attackdamage = thisThing.attackdamage;
    armor = thisThing.armor;
    Jarl_level_count = thisThing.Jarl_level_count;

}

public int getLevel() {
    return this.Jarl_level_count;
}

public void setLevel(int level) {
    this.Jarl_level_count = level;
}

@Override
public String toString() {
    String outputString;
    outputString = Jarl_level_count + "\n";

    return outputString;

}
}

JarlsofficeController class

public class JarlsofficeController implements Initializable {

Player you;
@FXML
TextArea prompt;
@FXML

Button Bill_rates;
@FXML
Button Ryan_hellgon;
@FXML
Button Victoria_dove;
@FXML
Button Game_Room;
@FXML
Button Exit;

public JarlsofficeController() {
    this.prompt = new TextArea();
    this.Bill_rates = new Button();
    this.Ryan_hellgon = new Button();
    this.Victoria_dove = new Button();
    this.Game_Room = new Button();

    this.Exit = new Button();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    prompt.setPrefSize(850, 50);
    you = new Player(0, 0, 0, 0, 0, 0);
    //prompt.setText(intro);
    prompt.setEditable(false);
}

  public void Ryan() {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setContentText("Jarl: Ahhhhhh..... adventurer what brings you here today? ");
    ButtonType yes = new ButtonType("I heard you may have some work?");
    ButtonType no = new ButtonType("Adventurer?");
    ButtonType Cancle = new ButtonType("Cancle");

    alert.getButtonTypes().setAll(yes, no, Cancle);
    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == yes) {
        alert.setHeight(600);
        alert.setContentText("Jarl: Ahhh yes I do. Long ago there was a proficy that an adventure would come to Davenrun and save Taledone from the forces of evil. A man who sets out to save what Davenrun was founded on… a haven of adventures like your self. \n Taladone is becoming dangours. I don’t have guards to spare the town. That is where you come in adventure. There are diffrent places among taladone that need to be explored. If you dare take upon the quest.");

        ButtonType yes2 = new ButtonType("What must I do?");
        ButtonType no2 = new ButtonType("That sounds dangourous?");

        alert.getButtonTypes().setAll(yes2, no2);
        Optional<ButtonType> result2 = alert.showAndWait();
        if (result2.get() == yes2) {
            alert.setHeight(600);
            alert.setContentText("Jarl: Go the Taladone Forrest for a start. There are mystroues creatures as well as a dungon that has an evil dragon that is ruling North Taladone.\n I’m afraid the dragon is going to leave the cave and attack the city we will all be doomed. Adventure. \n Go the forrest kill all the evil that haunts it and kill the dragon. If you can do that, I will pay you dearly.");

            ButtonType yes3 = new ButtonType("Yes");
            ButtonType no3 = new ButtonType("No");

            alert.getButtonTypes().setAll(yes3, no3);
            Optional<ButtonType> result3 = alert.showAndWait();

            if (result3.get() == yes3) {
                alert.setHeight(600);
                alert.setContentText("Jarl: Go to the gate and start your adventure!");

                you = new Player(0, 1, 0, 0, 0, 0);

                System.out.println(you.getLevel());

                ButtonType ok = new ButtonType("OK");
                alert.getButtonTypes().setAll(ok);
                Optional<ButtonType> new_result = alert.showAndWait();
            }
            if (result3.get() == no3) {
                alert.setHeight(600);
                alert.setContentText("Jarl: I am here if you change your mind...");
                ButtonType ok = new ButtonType("OK");
                alert.getButtonTypes().setAll(ok);
                Optional<ButtonType> new_result = alert.showAndWait();
            }
        }
        if (result2.get() == no2) {
            alert.setHeight(600);
            alert.setContentText("As is the rest of taladone it is I'm afraid. When I was a young boy my father took me on a boat and traveled the Irish sea to this Island, some sort of evil came and made the island worse and worse. \n The creates became increasly difficult and dangours. Now Taladone is an island of evil and we are in the center of it. I fear if noone stops it soon, we are all going to die....");
            ButtonType ok = new ButtonType("OK");
            alert.getButtonTypes().setAll(ok);
            Optional<ButtonType> new_result = alert.showAndWait();

        }

    }
    if (result.get() == no) {
        alert.setHeight(600);
        alert.setContentText("An adventure. A one who comes to the island to fight the forces of evil. \n A man who is wise and has no fear. A man who is brave and bold at the same time. A man who isn’t doesn’t fear death, they welcome.\n You have those qualities to save Taladone and the rest of the world.");
        ButtonType ok = new ButtonType("OK");
        alert.getButtonTypes().setAll(ok);
        result = alert.showAndWait();

    }
    if (result.get() == Cancle) {
        alert.setHeight(600);
        alert.setContentText("Well Alright then!");
        ButtonType ok = new ButtonType("OK");
        alert.getButtonTypes().setAll(ok);
        Optional<ButtonType> new_result = alert.showAndWait();
    }
    alert.getButtonTypes().setAll(yes, no, Cancle);
    alert.showAndWait();
}

public void exit() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("/Davenrun/Inside_Jarls_palace.fxml"));
    Stage Stage = new Stage();
    Stage.setTitle("jarls palace");
    Stage.setScene(new Scene(root, 1500, 200));

    Stage.show();
    closeWindow();
}

@FXML
public void closeWindow() {
    Stage stage = (Stage) this.Exit.getScene().getWindow();
    stage.close();
}

}

GateController Class

public class GateController implements Initializable {

@FXML
private TextArea prompt;
@FXML
private Button Northen_woods;
@FXML
private Button Eastern_Graveyard;
@FXML
private Button Southern_ocean;
@FXML
private Button Western_Moutnain;
@FXML
private Button Go_North;
@FXML
private Button Go_NorthWest;

public GateController() {
    this.prompt = new TextArea();
    this.Northen_woods = new Button();
    this.Eastern_Graveyard = new Button();
    this.Southern_ocean = new Button();
    this.Western_Moutnain = new Button();
    this.Go_North = new Button();
    this.Go_NorthWest = new Button();
}

public void Northen_woods() throws IOException {
    JarlsofficeController you = new JarlsofficeController();
    System.out.println(you.you.getLevel());
    if (you.you.getLevel() == 0) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Gate Guard");
        alert.setContentText("Gate Guard: You are not ready. I suggest you go talk to the Jalr first. ");

        ButtonType ok = new ButtonType("OK");
        alert.getButtonTypes().setAll(ok);
        alert.showAndWait();
    }
    if (you.you.getLevel() == 1) {

        Parent root = FXMLLoader.load(getClass().getResource("/The_northen_woods/Northen_bridge.fxml"));

        Stage stage = new Stage();

//        stage.setTitle("Outside house");
        stage.setScene(new Scene(root, 500, 500));
        stage.show();
        closeWindow();
      }

      }
    @FXML
  public void closeWindow() {
    Stage stage = (Stage) this.Eastern_Graveyard.getScene().getWindow();
    stage.close();
  }

Here is the fxml classes though it not needed for any reason other than compiling. Ignore the onAction errors, those are just methods I didn't post on here.

<?xml version="1.0" encoding="UTF-8"?>

 <?import java.lang.*?>
 <?import java.net.*?>
 <?import java.util.*?>
 <?import javafx.scene.*?>
 <?import javafx.scene.control.*?>
 <?import javafx.scene.layout.*?>

 <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
 styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" 
  fx:controller="Davenrun.GateController">
  <children>
    <TextArea fx:id="prompt" layoutX="100" layoutY="0.0" />
    <Button fx:id="Northen_woods" layoutX="100" layoutY="50.0" 
 mnemonicParsing="false" text="Go to Northen Woods" 
  onAction="#Northen_woods"  />
    <Button fx:id="Eastern_Graveyard" layoutX="100" layoutY="75.0" 
 mnemonicParsing="false" text="Go to Eastern Graveyard"  onAction = 
 "#Eastern_Graveyard"/>
    <Button fx:id="Southern_ocean" layoutX="100" layoutY="100.0" 
mnemonicParsing="false" text="Go to Southern_Ocean" 
onAction="#Southern_Ocean"  
/>       
    <Button fx:id="Western_Moutnain" layoutX="100" layoutY="125.0" 
 mnemonicParsing="false" text="Go to Western Mountain"  
 onAction="#Western_Montain"/>
    <Button fx:id = "Go_North" layoutX="100" layoutY="150.0" 
  mnemonicParsing="false" text="Go North" onAction ="#go_north"/> 
    <Button fx:id="Go_NorthWest" layoutX="100" layoutY="175.0" 
  mnemonicParsing="false" text="Go NorthWest" onAction ="#go_northWest"/>

  </children>
</AnchorPane>

Finally, here is the fxml for the jarls office

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="Davenrun.JarlsofficeController">
<children>

    <TextArea fx:id="prompt" layoutX="100" layoutY="0.0" text = "Welcome to 
the jarls office"/>
    <Button fx:id="Bill_rates" layoutX="100" layoutY="100.0" 
mnemonicParsing="false" text="Talk to Davenrun Guard" onAction = "#Bill"  />
    <Button fx:id="Ryan_hellgon" layoutX="100" layoutY="150.0" 
mnemonicParsing="false" text="Talk the Jarl of Davenrun" onAction = 
"#Ryan"/>
    <Button fx:id="Exit" layoutX="100" layoutY="200.0" 
   mnemonicParsing="false" text="Exit" onAction = "#exit"/>
   </children>
   </AnchorPane>

A possible solution would be great, I know I could use sql to work around this, but I'm trying to make this Software "Codecentric" rather than "Datacentric" and only use sql when I'm saving and loading the game because I want this Software have a bit a variety in java topics/concepts. I understand there are some logic errors in the jarl method by the method, software is still being developed and I'm trying to get the bare bones for now. Thanks for any help. enter code here

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • 3
    From a MVC perspective, your `Player` class is (part of?) the model. You need to create a single `Player` instance and pass it to each controller. See https://stackoverflow.com/questions/14187963/ for passing parameters to controllers in general, and https://stackoverflow.com/questions/32342864 for passing a model in a MVC-type design. You are really using dependency injection (DI) if you do this: each controller has a dependency on the `Player` instance. You might consider using a DI framework to simplify this aspect of the code. – James_D Nov 02 '17 at 12:56
  • I used the code. How do I "set" the value I wanna set? – bobbert_the_adventurer Nov 14 '17 at 06:52

0 Answers0