I'm sure this is fairly simple and I am misunderstanding something. I have a controller class linked to an FXML(view). The controller does not contain any statics, yet when I try and reference the model like this:
@FXML
public void goToConvert(MouseEvent event){
myController.setScreen(GameModel.CONVERT_SCREEN);
GameModel.CONVERTB = true;
GameModel.PLAYB = false;
GameModel.COMPAREB = false;
GameModel.SETTINGSB = false;
GameModel.HELPB = false;
GameModel.ProcessSreeenSwitch();
GameModel is my model
CONVERTB is a public boolean
ProcessScreeenSwitch() is a public void
Without taking any measures to include an object reference to GameModel in the controller, every line using GameModel creates the common error:
non-static variable CONVERTB cannot be referenced from a static context
Even though there are no statics in the controller whatsoever. I don't understand why the current context is static. I have tried declaring GameModel as an object above e.g (in the same place as ScreensController myController is declared in the code at the bottom)
GameModel GameModel = new GameModel();
or
GameModel GameModel;
These measures stop the static error but create a NullPointerException when they are called. For example, if goToConvert is called the NullPointerException is at the line:
GameModel.CONVERTB = true;
How can I make the current context non-static to change the values in the GameModel from the controller.
Cheers
Lucio
Controller Class:
package aimdot;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.File;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
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.input.MouseEvent;
import javafx.stage.Stage;
public class PlayScreenController implements Initializable,
ControlledScreen {
ScreensController myController;
//final static Image MOUSECURSOR = new Image (PlayScreenController.class.getResource("MouseCursor").toString());
//final static Image MOUSECURSOR_IMAGE = new Image (PlayScreenController.class.getResource("MouseCursor").toString());
// final static Image MOUSECURSOR_IMAGE = new Image (PlayScreenController.class.getResource("MouseCursor").toString());
@FXML
private ImageView imageView;
@FXML
private ImageView MouseCursor;
@FXML
private ImageView RectangleMouseHolder;
@FXML
private ImageView Target3Rings;
@FXML
private Label PLAYBUTTON;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
@FXML
private void PLAYBUTTONPressed(MouseEvent event){
PLAYBUTTON.setTranslateX(-400);
GameModel.AnimationTargetStart();
PLAYPHASE();
}
@FXML
public void TargetMissed(ActionEvent event){
}
@FXML
public void goToPlay(MouseEvent event){
myController.setScreen(GameModel.PLAY_SCREEN);
GameModel.CONVERTB = false;
GameModel.PLAYB = true;
GameModel.COMPAREB = false;
GameModel.SETTINGSB = false;
GameModel.HELPB = false;
GameModel.ProcessSreeenSwitch();
}
@FXML
private void goToMain(ActionEvent event){
myController.setScreen(GameModel.MAIN_MENU);
GameModel.CONVERTB = false;
GameModel.PLAYB = false;
GameModel.COMPAREB = false;
GameModel.SETTINGSB = false;
GameModel.HELPB = false;
GameModel.ProcessSreeenSwitch();
}
@FXML
public void goToConvert(MouseEvent event){
myController.setScreen(GameModel.CONVERT_SCREEN);
GameModel.CONVERTB = true;
GameModel.PLAYB = false;
GameModel.COMPAREB = false;
GameModel.SETTINGSB = false;
GameModel.HELPB = false;
GameModel.ProcessSreeenSwitch();
}
@FXML
public void goToCompare(MouseEvent event){
myController.setScreen(GameModel.COMPARE_SCREEN);
GameModel.CONVERTB = false;
GameModel.PLAYB = false;
GameModel.COMPAREB = true;
GameModel.SETTINGSB = false;
GameModel.HELPB = false;
GameModel.ProcessSreeenSwitch();
}
@FXML
public void goToSettings(MouseEvent event){
myController.setScreen(GameModel.SETTINGS_SCREEN);
GameModel.CONVERTB = false;
GameModel.PLAYB = false;
GameModel.COMPAREB = false;
GameModel.SETTINGSB = true;
GameModel.HELPB = false;
GameModel.ProcessSreeenSwitch();
}
@FXML
public void goToHelp(MouseEvent event){
myController.setScreen(GameModel.HELP_SCREEN);
GameModel.CONVERTB = false;
GameModel.PLAYB = false;
GameModel.COMPAREB = false;
GameModel.SETTINGSB = false;
GameModel.HELPB = true;
GameModel.ProcessSreeenSwitch();
}
@FXML
public void MouseLocation(MouseEvent event){
}
@FXML
public void RestartButton(MouseEvent event){
GameModel.RestartRound();
}
public void PLAYPHASE(){
boolean targetSpawned = false;
GameModel.playPhase = true;
long gameStartMS = System.currentTimeMillis();
long gameStartS = TimeUnit.MILLISECONDS.toSeconds(gameStartMS);
}
}
EDITED for addition steps tried.
tried using
GameModel Model = new GameModel();
in each controller, and eliminating use of GameModel fromt he controller but it didn't work. It still creates a null pointer exception at
Model.ProcessSreeenSwitch();
In GameModel class:
public void ProcessSreeenSwitch(){
System.out.print("PlayB: "+PLAYB);
if (PLAYB == false){
target.setVisible(false);
PlayLabel.setVisible(false);
PutMouseMiddle.setVisible(false);
hitCountG.setVisible(false);
missCountG.setVisible(false);
rectangleMouseBoxG.setVisible(false);
RestartPlayButton.setVisible(false);
PlayTimer.setVisible(false);
PlayTimerText.setVisible(false);
}
if (PLAYB == true){
target.setVisible(true);
PlayLabel.setVisible(true);
PutMouseMiddle.setVisible(true);
hitCountG.setVisible(true);
missCountG.setVisible(true);
rectangleMouseBoxG.setVisible(true);
RestartPlayButton.setVisible(true);
PlayTimer.setVisible(true);
PlayTimerText.setVisible(true);
}
}