0

I created an UI look using JavaFX scene builder and I can use it's controller and when I click a button it prints on the console.

My problem is how I can call other functions from the java program. When I create a new object in the controller to call other methods I am getting errors.

Here is a class I want to interact with:

   public class GOcommand extends Command {

    public GOcommand(String firstWord, String secondWord, String thirdWord) {
        super(firstWord, secondWord, thirdWord);  
    }

    public GOcommand() {}

    @Override
    public boolean execute(Player player) {
        if (!hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            Game.out.println(Game.messages.getString("goWhere"));
            return false;
        }

        String direction = getSecondWord();
        player.goRoom(direction);
        return false;
    }
    }

Here is the controller class:

    public class FXMLDocumentController implements Initializable {

    Player player;

    @FXML 
    private TextArea textArea;

     @FXML
    private Button northBtn;

    @FXML
    private Button giveBtn;

    @FXML
    private Button CloseBtn;

    @FXML
    private Button eastBtn;

    @FXML
    private Button dropBtn;

    @FXML
    private Button southBtn;

    @FXML
    private Button WestBtn;

    @FXML
    private Button helpBtn;

    @FXML
    private Button takeBtn;

    @FXML
    private void exitGame() {
       System.exit(1);
    }

    @FXML
    void northClicked(ActionEvent event) {
        textArea.appendText("Go North button Clicked\n");
        player.goRoom("NORTH");
    }

    @FXML
    private void eastClicked(ActionEvent event) {
        textArea.appendText("Go East button Clicked\n");
    }

    @FXML
    private void southClicked(ActionEvent event) {
        textArea.appendText("Go South button Clicked\n");
    }

    @FXML
    private void westClicked(ActionEvent event) {
        textArea.appendText("Go West button Clicked\n");
    }

    @FXML
    private void dropClicked(ActionEvent event) {

    }

    @FXML
    private void takeClicked(ActionEvent event) {
        textArea.appendText("Take button Clicked\n");
    }

    @FXML
    private void giveClicked(ActionEvent event) {
        textArea.appendText("Give button Clicked\n");
    }

    @FXML
    private void helpClicked(ActionEvent event) {
        textArea.appendText("Help me know!\n");
    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
    }

And these are the errors because I created the player:

 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 45 more
Caused by: java.lang.NullPointerException
    at assignment2.FXMLDocumentController.northClicked(FXMLDocumentController.java:62)
    ... 55 more
Amir
  • 8,821
  • 7
  • 44
  • 48
Pavlos
  • 906
  • 1
  • 11
  • 29
  • 3
    What errors do you get if you try to call 'other methods'? And could you post your attempt to call these other methods?(e.g. please post your controller class) – n247s Nov 30 '16 at 08:10

2 Answers2

0

At the bottom of the stacktrace you posted it shows an NPE, the field 'player' you are accesing. And from what I can see, that field isnt initilized. Otherwise this code would work perfectly fine.

n247s
  • 1,898
  • 1
  • 12
  • 30
  • I created it at the beginning "Player player;" in the controller @n247s – Pavlos Nov 30 '16 at 08:47
  • and this is the part of code in player class: public Player(String name, Room room) { this.name = name; currentRoom = room; items = new HashMap<>(); } – Pavlos Nov 30 '16 at 08:48
  • 1
    You created the field, but didnt initialized it. (E.g. `Player player = new Player();` – n247s Nov 30 '16 at 08:49
  • The problem is that player going through a game and he has name and room parameters, how Am I going to pass these parameters as well? @n247a – Pavlos Nov 30 '16 at 16:33
  • 1
    If you want just one player (or one place where player instances are handled) use a getter to get the right player instance. – n247s Nov 30 '16 at 17:23
  • The logic is the player walk through the rooms.. my main question is how to use buttons to go up right down left by using the buttons – Pavlos Nov 30 '16 at 22:24
  • instead of writing go up in console – Pavlos Nov 30 '16 at 22:25
0

You need to create a new instance of Player like:

Player player = new Player();

An InvocationTargetException in a JavaFX application is often caused by accessing fields which are null. Keep that in mind, when creating JavaFX applications!

braheem38
  • 23
  • 5
  • The problem is that player going through a game and he has name and room parameters, how Am I going to pass these parameters as well? – Pavlos Nov 30 '16 at 15:50