0

As the title suggest, I just want to separate (organize) the files inside my project, before everything I've read this:

  1. How to seperate Javafx(non fxml) to a fxml and a controller? (Invocation target exception)
  2. Is there a way to modularize a JavaFX application?
  3. How to seperate logic from controller JavaFx

This is the most approached answer.

  1. How to separate my gui in javafx?

But it's no quite clear for me still tho, (perhaps it's about I don't understand grammar cuz english is not my native lang).

I tried to replicate the last one manually, but still can't figure how should I point to the fxml files.

Here is my structure:

My project Structure

The code for my main app:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
/**
 * Principal
 * @author Adrian
 *
 */
public class Login extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
            Scene scene = new Scene(root,300,200);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setTitle("Login");
            primaryStage.setScene(scene);
            primaryStage.show();
            Image icono = new Image(getClass().getResourceAsStream("/SiLi/src/main/recursos/img/usuario.gif"));
            primaryStage.getIcons().add(icono);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

The error I get:

> 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 org.ajfmo.sili.core.Login.start(Login.java:19)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    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)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
AjFmO
  • 395
  • 2
  • 4
  • 18
  • You can find more and more Q/A like what you want.For your error you should verify your path of fxml file. – Menai Ala Eddine - Aladdin Aug 30 '17 at 19:56
  • Possible duplicate of [Applying MVC With JavaFx](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) – Menai Ala Eddine - Aladdin Aug 30 '17 at 19:59
  • see https://stackoverflow.com/questions/35630415/nullpointer-location-on-javafx-fxmlloader and https://stackoverflow.com/questions/34828952/jafafx-load-fxml-file-error-null-pointer-exception – Menai Ala Eddine - Aladdin Aug 30 '17 at 20:10
  • Surely it should be `getClass().getResource("/fxml/login.fxml")`? Not sure what the exception has to do with MVC, you just have the wrong path to the FXML file. – James_D Aug 30 '17 at 20:17
  • @James_D thank you,that solved part of the trouble, and another issue with class not found exception because I was not providing the proper controller path to the fxml, I had to address with whole package name, I'm trying to learn by myself since where I live there's no good formation on programing. – AjFmO Aug 30 '17 at 20:40
  • @MenaiAlaEddine Thank you, I gathering the provided information and studying it. Thanks. – AjFmO Aug 30 '17 at 20:42

1 Answers1

0

I solved this issue folowing James_D comment, still not clear to me why this happen (I guess it have something to do with absolute and relative paths and the current working directory). Anyhow I managed to organize my project into correct folders.

Using the information provided by James:

Surely it should be getClass().getResource("/fxml/login.fxml")? Not sure what the exception has to do with MVC, you just have the wrong path to the FXML file. – James_D

And pointing at the correct path or location of my "Login.java" class controller in the Login.fxml file, try to point to the package container of the controller like this:

fx:controller="org.ajfmo.sili.controlador.CLogin"

Allowed my to solve the issue and keep in the track...

Thanks for helping me out.

AjFmO
  • 395
  • 2
  • 4
  • 18