Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resolved Invalid Input Stream for .fxml File (JavaFX)

Joined
Feb 15, 2016
Messages
64
Hey guys,

Got a quick question. I'm trying to make a startup GUI for my bot using JavaFX. Had some troubles using JavaFX at first but a quick Google search fixed that. My current problem is that the input stream for my .fxml file is apparently null even though it shouldn't be. I'll add my code and an image of my project layout which should do more talking than my words.

Thanks.

GUI File
(note: I also tried other paths like "/RuneMate/src/com/smitty260/cowkiller/CowKiller.fxml" and other variants like that)
Code:
package com.smitty260.cowkiller;

import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class CowKillerGui extends Stage {
   
    public CowKillerGui (CowKiller script) {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setController(new CowKillerController(this));
            final Parent root = loader.load(CowKiller.class.getResourceAsStream("CowKiller.fxml"));
            final Scene scene = new Scene(root);
            setTitle("Cow Killer");
            setScene(scene);
        } catch (IOException e) {
            e.printStackTrace();
        }
        show();
    }
   
}

Project Layout
asdasddd.JPG
 
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
Code:
final Parent root = loader.load(Resources.getUrl("/com/smitty260/cowkiller/CowKiller.fxml"));
 
Joined
Dec 10, 2014
Messages
3,255
Tried that and I got this error: "java.lang.NullPointerException: Location is required."
Try Resources.getAsStream("/com/smitty260/cowkiller/CowKiller.fxml"), or if that doesn't work then try
CowKiller.class.getResourceAsStream("/com/smitty260/cowkiller/CowKiller.fxml"), although it's encouraged to use the Resources class, it doesn't work for some people. Also, have you added your project classpath or not? IntelliJ does it automatically iirc, but for some others, and for starting from a batch file, you need to define it properly.
 
Last edited:
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
Its probably nothing about the resource itself, but rather some error while initializing the controller. Look at the full stack trace and see if it gets caused by some other exception.
 
Joined
Feb 15, 2016
Messages
64
Try Resources.getAsStream("/com/smitty260/cowkiller/CowKiller.fxml"), or if that doesn't work then try
CowKiller.class.getResourceAsStream("/com/smitty260/cowkiller/CowKiller.fxml"), although it's encouraged to use the Resources class, it doesn't work for some people. Also, have you added your project classpath or not? IntelliJ does it automatically iirc, but for some others, and for starting from a batch file, you need to define it properly.

Tried both of those suggestions, but both give the same NullPointerException error. I'm not too sure about the classpath thing. I'm using Eclipse Luna. How would I check that? Haha. Probably a silly question. There's a .project and .classpath file in the project's actual folder.

Its probably nothing about the resource itself, but rather some error while initializing the controller. Look at the full stack trace and see if it gets caused by some other exception.

Well this is the full thing:

Code:
java.lang.NullPointerException: inputStream is null.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at com.smitty260.cowkiller.CowKillerGui.<init>(CowKillerGui.java:18)
    at com.smitty260.cowkiller.CowKiller.lambda$0(CowKiller.java:38)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

It seems like it's something to do with initialising the .fmxl file to me but I'm not too sure.
 
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
Tried both of those suggestions, but both give the same NullPointerException error. I'm not too sure about the classpath thing. I'm using Eclipse Luna. How would I check that? Haha. Probably a silly question. There's a .project and .classpath file in the project's actual folder.



Well this is the full thing:

Code:
java.lang.NullPointerException: inputStream is null.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at com.smitty260.cowkiller.CowKillerGui.<init>(CowKillerGui.java:18)
    at com.smitty260.cowkiller.CowKiller.lambda$0(CowKiller.java:38)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

It seems like it's something to do with initialising the .fmxl file to me but I'm not too sure.
Not if there's nothing in the stack trace indicating it...
 
Joined
Jul 6, 2014
Messages
24
Don't lead your relative paths with a file separator.
Code:
Resources.getAsStream("com/smitty260/cowkiller/CowKiller.fxml"),
should be fine.
 
Joined
Feb 15, 2016
Messages
64
Not if there's nothing in the stack trace indicating it...

I dunno then lol. I just thought it would have been that because that's the line of the file that the error references.

Don't lead your relative paths with a file separator.
Code:
Resources.getAsStream("com/smitty260/cowkiller/CowKiller.fxml"),
should be fine.

Didn't work either. xD
Don't think the file seperator at the front makes a difference. :s
 
Joined
Feb 15, 2016
Messages
64
It technically does, to indicate an absolute or relative path from the class which is actually creating the URL.

Oh, well there ya go haha. Didn't work though. :(
 
Okay, turns out I didn't add it to the .XML file as a resource. Derp. That's embarrassing.
 
Top