1

IntelliJ Idea is giving me this warning:

Argument 'getClass().getClassLoader().getResource( "view/RootLayout.fxml" )' might be null

However, it always works. I just want the clear warning, preferably without annotations or disabling the inspection.

Per this thread: IntelliJ IDEA - getClass().getResource("...") return null

  • I have my resources folder set as a Resources Root.
  • I added *.fxml to Settings -> Build, Execution, Deployment -> Compiler -> Resource patterns
  • I even added ?.fxml and !?.fxml to cover more possibilities.

None of those seem to make any difference.

Here is my start() method:

@Override
public void start( Stage primaryStage ) throws Exception
{
    // Load the FXML file containing all of the UI elements.
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation( getClass().getResource( "view/RootLayout.fxml" ) );
    Parent root = FXMLLoader.load( getClass().getClassLoader().getResource( "view/RootLayout.fxml" ) );

    // Create the stage and set the window title.
    primaryStage.setTitle( "SNMP Link Utilization" );

    // Set the scene using root, with the specified width and height.
    primaryStage.setScene( new Scene( root, 500, 600 ) );

    // Set the icon for a non-Maven build.
    //primaryStage.getIcons().add( new Image( "file:resources/images/nic.png" ) );
    // Set the icon for a Maven build.
    primaryStage.getIcons().add( new Image( "images/nic.png" ) );

    primaryStage.show();
}

Am I doing anything wrong? I've tried a few other ways of creating the Parent object, but all that I have tried end up failing.

Adam Howell
  • 415
  • 1
  • 11
  • 24
  • 1
    That just looks like an over-zealous ide warning to me. I don't know IntelliJ very well but I suspect there's a way to configure that. You could put the result of the call to `getResource(...)` in a variable url and wrap the rest of your code in an `if (url != null) { ... }` (or just throw an exception if it is null), and maybe that would appease the warning, but that seems overkill. – James_D Jun 05 '17 at 21:46

1 Answers1

1

You can get rid of that warning if you refactor the code a little bit this way:

    FXMLLoader loader = new FXMLLoader(getClass().getResource(
            "view/RootLayout.fxml"));
    Parent root = loader.load();
Sunflame
  • 2,993
  • 4
  • 24
  • 48
  • This works, if I change the FXMLLoader line to this: `FXMLLoader loader = new FXMLLoader( Main.class.getResource( "../../../view/RootLayout.fxml" ) );` Do you know why it "loses sight" of my Resources Root location? I find this code kind of ugly. Is there a way to get back to the shorter name, without the updirectory indicators? – Adam Howell Jun 06 '17 at 14:43
  • 1
    So here is my code that works for me. https://pastebin.com/GcxG8U2u. So both `Test.class` and `Test.fxml` are in the test package and it finds the `.fxml` file without any exception. – Sunflame Jun 06 '17 at 15:06