1

I am trying to move an imageView. I always get a null pointer exception even though the puls is still moving my monster to my mannetje. I think the problem is that I make a new controller. How can I call the method without making a new controller or to fix the null pointer exception.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

public class main2 extends Application{

public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("setup.fxml"));
      primaryStage.setTitle("TFOA");
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();

        controller control = new controller();

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                case UP:    control.up(); break;
                case DOWN:  control.up(); break;
                case LEFT:  control.up(); break;
                case RIGHT: control.up(); break;
                }
            }
        });
        scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                    case UP:    control.up(); break;
                    case DOWN:  control.up(); break;
                    case LEFT:  control.up(); break;
                    case RIGHT: control.up(); break;
                }
            }
        });


}

public static void main(String[] args)  {

     launch(args);

}

this is my main

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
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.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class controller{ 
public Button newGame;
public Button cont;
public AnchorPane pane;
public ImageView mannetje;
public Circle monster;
public  Scene scene;
public Image boat; 
public Label hitpoints;
Hero hero;
Monster monsterChar;
private Timeline timeline;
private int countdown = 15; 


public void initialize(){

 timeline = new Timeline(new KeyFrame(Duration.seconds(0.7),ob -> puls()));
  timeline.setCycleCount(15000);

}
private void puls() {

if(mannetje.getLayoutX()<monster.getLayoutX() && mannetje.getLayoutY() == monster.getLayoutY()){ // LEFT
    monster.setLayoutX(monster.getLayoutX()-10);

    if((mannetje.getLayoutX()+mannetje.getFitWidth()+(monster.getRadius()/2))>monster.getLayoutX()){
        monsterChar.hit(hero);
    }

}
if(mannetje.getLayoutX()<monster.getLayoutX() && mannetje.getLayoutY() < monster.getLayoutY()){ // LEFT-UP
    monster.setLayoutX(monster.getLayoutX()-10);
    monster.setLayoutY(monster.getLayoutY()-10);
}
if(mannetje.getLayoutX()==monster.getLayoutX() && mannetje.getLayoutY() < monster.getLayoutY()){ // DOWN
    monster.setLayoutY(monster.getLayoutY()+10);

    if((mannetje.getLayoutY()-mannetje.getFitHeight()-(monster.getRadius()/2))>monster.getLayoutY()){
        monsterChar.hit(hero);
    }

}
if(mannetje.getLayoutX()==monster.getLayoutX() && mannetje.getLayoutY() > monster.getLayoutY()){ // UP
    monster.setLayoutY(monster.getLayoutY()-10);

    if((mannetje.getLayoutY()+mannetje.getFitHeight()+(monster.getRadius()/2))>monster.getLayoutY()){
        monsterChar.hit(hero);
    }

}
if(mannetje.getLayoutX()>monster.getLayoutX() && mannetje.getLayoutY() > monster.getLayoutY()){ // RIGHT-UP
    monster.setLayoutX(monster.getLayoutX()+10);
    monster.setLayoutY(monster.getLayoutY()+10);
}

if(mannetje.getLayoutX()>monster.getLayoutX() && mannetje.getLayoutY() < monster.getLayoutY()){ // RIGHT-DOWN
    monster.setLayoutX(monster.getLayoutX()+10);
    monster.setLayoutY(monster.getLayoutY()-10);
}
if(mannetje.getLayoutX()<monster.getLayoutX() && mannetje.getLayoutY() > monster.getLayoutY()){ //LEFT-DOWN
    monster.setLayoutX(monster.getLayoutX()-10);
    monster.setLayoutY(monster.getLayoutY()+10);
}
if(mannetje.getLayoutX()>monster.getLayoutX() && mannetje.getLayoutY() == monster.getLayoutY()){ // RIGHT
    monster.setLayoutX(monster.getLayoutX()+10);

    if((mannetje.getLayoutX()-(mannetje.getFitWidth()/2))<monster.getLayoutX()){
        monsterChar.hit(hero);
    }

}
}

public void newGame(){
    cont.setVisible(false);
    newGame.setVisible(false);
    pane.setStyle("-fx-background-image: url('map.jpg');");
    //System.out.println(mannetje);
    mannetje.setVisible(true);
    monster.setVisible(true);
    hitpoints.setVisible(true);
    //moveCircleOnKeyPress();
    System.out.println(mannetje);
    //boat.setImage(null);
    //boat.setStyle("-fx-text-fill: white;");
    hero = new Hero("Klud" , 1500);
    monsterChar = new Monster("Chak Al Ghui",50,7);
    hitpoints.setText(hero.getName() + ": " + hero.getEffectiveHitpoints()+"          "+monsterChar.getName()+":  "+ monsterChar.getEffectiveHitpoints() ) ;
    Weapon lition = new Weapon(2,"Lition");
    lition.setDamage(7);
    Armor faciate = new Armor(10,"Faciate");
    faciate.setEffectiveProtection(1);
    Backpack backpack = new Backpack(1, "Backpack",500);
    hero.equip(backpack, Anchor.back);
    hero.equip(lition, Anchor.rightHand);
    hero.equip(faciate, Anchor.body); 
      timeline.playFromStart();
      mannetje.setLayoutY(200);
      monster.setLayoutY(200);
}

public void up(){
    mannetje.setLayoutY(mannetje.getLayoutY()-25);
}
public void left(){
    mannetje.setLayoutX(mannetje.getLayoutX()-25);
}
public void right(){
    mannetje.setLayoutX(mannetje.getLayoutX()+25);
}
public void down(){
    mannetje.setLayoutY(mannetje.getLayoutY()+25);
}
public void hit(){

    if((mannetje.getLayoutX()+mannetje.getFitWidth()+monster.getRadius())>monster.getLayoutX()){
    hero.hit(monsterChar);
}
    else{
    }
    hitpoints.setText(hero.getName() + ": " + hero.getEffectiveHitpoints()+"          "+monsterChar.getName()+":  "+ monsterChar.getEffectiveHitpoints() ) ;

}

}

this is the controller class.

Alan Maene
  • 11
  • 3
  • As you (kind of) observe, the controller is a *specific object* that is created by the FXML loader. The object you created and called `control` is not the controller (it's just another object that happens to be from the same class). See http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 for various techniques for communicating with the controller. – James_D May 08 '17 at 20:12

0 Answers0