0

We're writing an Java application using JavaFX. At this time we have 3 different forms:

  • Login
  • Game window
  • Registration

For our next iteration, we want to implement the Registration form, but we get the IOException error Unknown Path

It's about this piece of code:

FXMLLoader registrationLoader = new FXMLLoader(); try{ mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream()); Stage registrationStage = new Stage(); Scene scene = new Scene(mainroot); registrationStage.setScene(scene); registrationStage.setTitle("Register your account"); registrationStage.show(); } catch(IOException ex) { System.out.print(ex.getMessage()); } 

The above code is working when I change FXMLRegistration.fxml to FXMLDocument.fxml or FXMLLoader.fxml.

When I change

mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream()); 

to

mainroot = (Parent)registrationLoader.load(Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL()); 

source

I get the absolute path in the debugger output, which is correct when I use it with the file command in terminal.

I hope someone could help us with this error.

Thanks in advance!

EDIT

I Changed some code to the following:

FXMLLoader registrationLoader = new FXMLLoader(getClass().getResource("/FXMLRegistration.fxml")); mainroot = (Parent)registrationLoader.load(); 

but this will return an IllegalStateException: Location is not set. When I remove / before /FXMLRegistration.fxml, I get to my catch block printing the full path of the file:

file:/Users/juleskreutzer/Documents/github/PTS3/HackAttackFX/dist/run1793658053/HackAttackFX.jar!/hackattackfx/FXMLRegistration.fxml

Also changing the path to src/hackattackfx/FXMLRegistration.fxml will give the IllegalStateException: Location not set.

Project Structure

We use different packages in our application. all these packages are within the default package: hackattackfx

The packages in the default package are:

  • Default Package
    • Exceptions
    • Interfaces
    • enums
    • Resources
    • Templates
  • JSON Package

My FXML documents are located in the default package (hackattackfx). If it's not 100% clear how I arranged my files, please take a look at my Github repo

4
  • Your question is not very clear (to me). When you are using Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL() does it work? Is you aim to use Paths.get() ? If yes, why? Commented Nov 9, 2015 at 17:47
  • Paths.get() does not work, it prints the path to my debugger so it shouls get in my catch block. If possible, I want to use getClass().getResource("X").openStream() Commented Nov 9, 2015 at 18:00
  • You should use getClass().getResource() if you file is present on the classpath. Paths.get() doesn't make any sense under such circumstances. Commented Nov 9, 2015 at 18:01
  • When I use getClass().getResource(), the path also gets printed, but than it is ../HackAttack.jar!/hackattackfx/FXMLRegistration.fxml. I can't find this path in terminal, I think because the ! Commented Nov 9, 2015 at 18:03

2 Answers 2

1

So, I got all curious to find out the root cause, I cloned the repo and found that the actual issue was the following error and the one that was posted by the OP in the question

Caused by: java.lang.NullPointerException at hackattackfx.FXMLRegistrationController.initialize(FXMLRegistrationController.java:67)

Which means that in the controller pane was null.

This was because the fxml was missing a declaration of fx:id .

Add fx:id="pane" to the AnchorPane declaration of FXMLRegistration.fxml and things should just work fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. I'll try it when i'm back at my desk and will let you know if it worked!
Sorry, but your answers doesn't work for me. I edited my question with the results after trying your answer.
Can you add the project structure to the question?
0

You need to start your Path with /

this is working for me:

final String fxmlPath = "/fxml/Main.fxml"; final FXMLLoader loader = new FXMLLoader(this.getClass().getResource(fxmlPath)); 

Main.fxml is located in the resource folder (for me: /src/main/resources/fxml/)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.