Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX "Location is required." even though it is in the same package

Tags:

java

javafx-2

I am trying to get my JavaFX program to run but am having some difficulty. I keep getting an error of 'java.lang.NullPointerException: Location is required.' The fxml file is in the same package as Application class. Here is my very simple code:

package com.kromalights.designer.entry;  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) throws Exception{         Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));         primaryStage.setTitle("Kromalights Designer");         primaryStage.setScene(new Scene(root, 300, 275));         primaryStage.show();     }       public static void main(String[] args) {         launch(args);     } } 

And here is a copy of my main.fxml file:

<?xml version="1.0" encoding="UTF-8"?>  <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.BorderPane?> <?scenebuilder-stylesheet mailStyles.css?> <?import java.net.*?>  <BorderPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1"         xmlns="http://javafx.com/javafx/2.2"         fx:controller="com.kromalights.designer.entry.Controller">     <bottom>         <Pane prefHeight="200.0" prefWidth="200.0"/>     </bottom>     <center>         <Pane prefHeight="200.0" prefWidth="200.0"/>     </center>     <left>         <VBox prefHeight="200.0" prefWidth="100.0"/>     </left>     <top>         <HBox prefHeight="100.0" prefWidth="200.0"/>     </top>     <stylesheets>         <URL value="@mainStyles.css" />     </stylesheets> </BorderPane> 

The controller class does exist and is in the package specified in the fxml file. All of my names are correct and are where I think they should be. What am I missing? I did try renaming my fxml file in case it was a name issue. Please help. FYI, I am using Intellij IDEA on OSX.

UPDATE: This is a Maven issue. I setup Maven for this project and that caused the issue. I removed Maven temporarily so I can continue working without it. Does anyone have any insight as to how I would best handle this when using Maven?

like image 930
Gremash Avatar asked Dec 10 '13 23:12

Gremash


People also ask

Can Javafx work without Scene Builder?

You don't have to use FXML or SceneBuilder. You can simply create the objects yourself and add them to your Scene/Stage yourself. It's entirely open as to how you implement it. I've implemented a Screen Management library where it handles either FXML or manually created screens.

Is Javafx a package?

The package javafx. beans. property defines read-only properties and writable properties, plus a number of implementations. Provides the set of classes for javafx.

What is FXML loader?

public class FXMLLoader extends Object. Loads an object hierarchy from an XML document.

What does init do in Javafx?

The initialize() method is used to initialize any controls, especially when it is something that can't be done from the FXML. An example of this is setCellFactory() and setCellValueFactory() . If you have nothing to initialize, then you do not need to put an initialize() method in your controller class.


1 Answers

In my case all of the above were not the problem at all.

My problem was solved when I replaced :

getClass().getResource("ui_layout.fxml") 

with :

getClass().getClassLoader().getResource("ui_layout.fxml") 
like image 52
Amir Arad Avatar answered Oct 18 '22 18:10

Amir Arad