0

I have an FXML file that is in a separate package. this answer is close but it is located in the same package.

the what confuses me is

  1. Where is this path /src/resources/.fxml that some of the answers reference
  2. How do I "copy" my fxml code into it. This is the line of code that is getting me Nullpointerexception: Location is Required

    root = FXMLLoader.load(getClass().getResource("IMSView/AppView.fxml"));
    

I have tried it with

root = FXMLLoader.load(getClass().getResource("/IMSView/AppView.fxml"));

Still get the same failure.

I am very new to java and I know I am doing something wrong. I could get this project to run when everything was in one file path. Now that I have added packages I cannot get the fxml to load.

Thanks for your time.

EDIT

I am using netbeans The Main method ins in IMS.java The FXML that I want to load is in a package called IMSview and is name AppView.fxml Project files

My Code

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ims;

import IMSModel.Product;
import IMSModel.Part;
import IMSView.AddProductController;
import IMSView.AppViewController;
import IMSView.AddPartController;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


/**
 *
 * @author DBarnett
 */
public class IMS extends Application {

    private Stage primaryStage;
    private Stage window;
    private BorderPane rootLayout;
    private ObservableList<Part> PartData = FXCollections.observableArrayList();
    private ObservableList<Product> ProductData = FXCollections.observableArrayList();

    public IMS() {

        // Add some sample data
//       PartData.add(new InHouse(1,"Silly Cow",1,23));
//       ProductData.add(new OutSourced(2"dog",2,3));
    }

    /**
     * Returns the data as an observable list of Parts.
     *
     * @return
     */
    public ObservableList<Part> getPartData() {
        return PartData;
    }

    public ObservableList<Product> getProductData() {
        return ProductData;
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
         window = primaryStage;
         Parent root;
        root = FXMLLoader.load(getClass().getResource("IMSView/AppView.fxml"));

        Scene scene = new Scene(root);
        primaryStage.setTitle("IMS");
        primaryStage.setScene(scene);
        primaryStage.show();

//        this.primaryStage = primaryStage;
//        this.primaryStage.setTitle("Inventory Management System");
        window.setOnCloseRequest(e -> {
            e.consume();
            closeProgram();
        });
  • 1
    Post the project structure in your question. Which package is the class from which you are calling `FXMLLoader.load()` in, and which packagei sthe FXML file in? – James_D Apr 12 '17 at 20:24
  • As for the `resources` folder, some IDEs and/or build tools encourage the use of a separate folder called `resources` for resources the application needs to access at runtime that are not Java source code files (so, essentially, things that are not compiled, but that still need to be deployed with the application, such as images, properties files, etc). You don't *need* a separate resources folder, and tbh I find using it for FXML files in JavaFX applications a bit of a hinderance (I am probably in a minority of ~1 in that opinion). – James_D Apr 12 '17 at 20:31
  • did you try "../IMSView/AppView.fxml"? – SedJ601 Apr 12 '17 at 21:52
  • @SedrickJefferson Don't use `..` as part of a resource name. It will not work if/when you bundle the application as a jar file. – James_D Apr 12 '17 at 22:02
  • @James_D Thanks for your responses I keep seeing answers like this. [link](https://stackoverflow.com/a/26452046/7852764 ) I am just not clear on how to implement that in netbeans. When I first built this up It was very messy and thus I wanted to separate things out abit. If I cannot find an answer I might have to put everything in on location again. – Urgeoverkill Apr 13 '17 at 01:58
  • The second code snippet you posted (with the forward slash at the beginning: `"/IMSView/AppView.fxml"`) should work. I don't use netbeans, so I'm not sure on the details of how to do this, but I would check that the FXML files are being deployed to the build folder (which is likely called `out`, `build`, or `bin`, or some such): there should be an `IMSView` folder there with the FXML file in it (along with `AppViewController.class`). Another approach, which still (of course) depends on the FXML being deployed, is to use `FXMLLoader.load(AppViewController.class.getResource("AppView.fxml"));`. – James_D Apr 13 '17 at 02:04

0 Answers0