Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/exception in thread "javafx application thread" java.lang.nullpointerexception. How to fix?
Next
Answered
Margarita Cronin
  • 2
Margarita Cronin
Asked: July 4, 20222022-07-04T15:30:41+00:00 2022-07-04T15:30:41+00:00In: java

exception in thread “javafx application thread” java.lang.nullpointerexception. How to fix?

  • 2

. Advertisement .

..3..

. Advertisement .

..4..

I get this message: exception in thread “javafx application thread” java.lang.nullpointerexception in my JavaFX program.

This is my code:

package application;


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



public class Main extends Application {
@Override
public void start(Stage stage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Scene.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
Scene scene = new Scene(root);

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {



@Override
public void handle(KeyEvent event) {
//System.out.println(event.getCode());
//if I write -> if(controller != null) it doesn't work
switch(event.getCode()) {
case UP:
controller.moveUp();
break;
case DOWN:
controller.moveDown();
break;
case LEFT:
controller.moveLeft();
break;
case RIGHT:
controller.moveRight();
break;
default:
break;

}
}

});

stage.setScene(scene);
stage.show();

} catch(Exception e) {
e.printStackTrace();
}

}

public static void main(String[] args) {
launch(args);
}
}

And controller:

package application;


public class Controller {

public void moveUp() {
System.out.println("UP");
}

public void moveDown() {
System.out.println("DOWN");
}

public void moveLeft() {
System.out.println("LEFT");
}

public void moveRight() {
System.out.println("RIGHT");
}

}

If you are aware of the solution. Help me, please.

  • 1 1 Answer
  • 100 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-07-04T15:41:27+00:00Added an answer on July 4, 2022 at 3:41 pm

    The cause: We can see the clear reason that the exception indicates: because “this.val$controller” is null. That why the message error: exception in thread “javafx application thread” java.lang.Nullpointerexception appears.

    Solution:

    The controller can be adjusted in one of two ways:

    Before using the load command, explicitly execute setController on the fxml loader instance.

    Or:

    Set the FXML’s fx:controller attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.