0

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)
James_D
  • 201,275
  • 16
  • 291
  • 322
Faizan Ul Haq
  • 454
  • 2
  • 7
  • 23
  • What if I told you that `getClassLoader` doesn't read from the package where your source code is located? – OneCricketeer May 05 '16 at 12:44
  • i was having same error before after searching stack overflow i reached a sloution to add getclassloader() http://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package – Faizan Ul Haq May 05 '16 at 12:54
  • same code was working fine few hours ago ! – Faizan Ul Haq May 05 '16 at 12:54

2 Answers2

2

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.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • yes it is being exported and both of these solutions are giving same error . is it possible some thing is wrong with my machine ? – Faizan Ul Haq May 05 '16 at 13:25
  • javafx.fxml.LoadException: /Users/myname/Documents/workspace/Javafx%20tesr/bin/application/Main.fxml:7 – Faizan Ul Haq May 05 '16 at 13:29
  • That isn't the "same error" though. Why did you say you were getting the "same error"? Since the previous error message was reporting that it couldn't find the fxml file, and the new error message is saying it has found an error on line 7 of the fxml file, I think you can safely assume this fixed the original error, no? – James_D May 05 '16 at 13:30
  • not same as previous but new same error .. i was going to write both thing in one comment but got splitted due to mistake .. so new error is in xml file now ? – Faizan Ul Haq May 05 '16 at 13:33
  • That's what the error message says, yes. At line 7. – James_D May 05 '16 at 13:42
0
try{    
scene.getStylesheets().add(new File(pathTocssFile).toURI().toURL().toExternalForm());
} catch (MalformedURLException e) {
e.printStackTrace();}
Navaneeth
  • 116
  • 1
  • 3
  • i am not using css file in it . .(sorry new to GUI only know basics of swing) – Faizan Ul Haq May 05 '16 at 13:00
  • instead of FXMLLoader.load(getClass().getClassLoader().getResource("/Main.fxml")); try FXMLLoader.load(new File(pathToFXMLFile).toURI().toURL().toExternalForm()); – Navaneeth May 05 '16 at 13:08
  • "new File" write it as it is ? because it is asking to import java.io wich creates another error and path to xml file "srx/application/main.fxml"? – Faizan Ul Haq May 05 '16 at 13:15