javaFX, throws NullPointerException, Location is required

前端 未结 2 1621
借酒劲吻你
借酒劲吻你 2021-01-28 03:12

i have seen other answers but nothing have helped me

(sorry new to GUI only know basics of swing)

this is main class

package application;

imp         


        
2条回答
  •  悲哀的现实
    2021-01-28 03:51

    getClass().getClassLoader().getResource(...) will load a resource from a path relative to the classpath. Since you placed the FXML file in the application pacakge, you need:

    Parent root=FXMLLoader.load(getClass().getClassLoader().getResource("application/Main.fxml"));
    

    If you just use getClass().getResource(...), and do not prefix the path with /, it will load from a path relative to the current class. So

    Parent root=FXMLLoader.load(getClass().getResource("Main.fxml"));
    

    should also work.

    Make sure that your FXML file is being exported to the build folder, along with the .class files.

提交回复
热议问题