javaFX, throws NullPointerException, Location is r

2019-03-04 00:56发布

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;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
Parent root=FXMLLoader.load(getClass().getClassLoader().getResource("/Main.fxml"));

  Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } 

    catch(Exception e) {
        e.printStackTrace();
        System.exit(0);

    }
}

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

this is another class whose method i want to invoke

package application;


import java.util.Random;
import javafx.event.ActionEvent;

import javafx.fxml.FXML;
import javafx.scene.control.Label;


public class MainController {

@FXML
private Label myMessage;
void generaterandom (ActionEvent event){
    Random rand=new Random();
    int myrand=rand.nextInt(50)+1;
    myMessage.setText(Integer.toString(myrand));
    System.out.println(Integer.toString(myrand));


}

}

i will add xml file if needed .

enter image description here

this is the error i am getting

java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:14)
at     com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl    .java:863)
    at     com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:32    6)
    at     com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at     com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at     com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

2条回答
看我几分像从前
2楼-- · 2019-03-04 01:18
try{    
scene.getStylesheets().add(new File(pathTocssFile).toURI().toURL().toExternalForm());
} catch (MalformedURLException e) {
e.printStackTrace();}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-04 01:19

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.

查看更多
登录 后发表回答