javaFX, throws NullPointerException, Location is required

前端 未结 2 1529
借酒劲吻你
借酒劲吻你 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:50
    try{    
    scene.getStylesheets().add(new File(pathTocssFile).toURI().toURL().toExternalForm());
    } catch (MalformedURLException e) {
    e.printStackTrace();}
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题