Resolved Invalid Input Stream for .fxml File (JavaFX)

Discussion in 'Developer Support' started by smitty260, Feb 20, 2016.

  1. 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 (Text):
    1.  
    2. package com.smitty260.cowkiller;
    3.  
    4. import java.io.IOException;
    5.  
    6. import javafx.fxml.FXMLLoader;
    7. import javafx.scene.Parent;
    8. import javafx.scene.Scene;
    9. import javafx.stage.Stage;
    10.  
    11. public class CowKillerGui extends Stage {
    12.    
    13.     public CowKillerGui (CowKiller script) {
    14.         try {
    15.             FXMLLoader loader = new FXMLLoader();
    16.             loader.setController(new CowKillerController(this));
    17.             final Parent root = loader.load(CowKiller.class.getResourceAsStream("CowKiller.fxml"));
    18.             final Scene scene = new Scene(root);
    19.             setTitle("Cow Killer");
    20.             setScene(scene);
    21.         } catch (IOException e) {
    22.             e.printStackTrace();
    23.         }
    24.         show();
    25.     }
    26.    
    27. }
    28.  

    Project Layout
    asdasddd.JPG


     
  2. Best Answer:
    Post #11 by smitty260, Feb 21, 2016
  3. Code (Text):
    1. final Parent root = loader.load(Resources.getUrl("/com/smitty260/cowkiller/CowKiller.fxml"));
     
  4. Tried that and I got this error: "java.lang.NullPointerException: Location is required."
     
  5. 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.
     
    #4 SlashnHax, Feb 21, 2016
    Last edited: Feb 21, 2016
  6. 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.
     
  7. 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 (Text):
    1.  
    2. java.lang.NullPointerException: inputStream is null.
    3.     at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    4.     at javafx.fxml.FXMLLoader.load(Unknown Source)
    5.     at com.smitty260.cowkiller.CowKillerGui.<init>(CowKillerGui.java:18)
    6.     at com.smitty260.cowkiller.CowKiller.lambda$0(CowKiller.java:38)
    7.     at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    8.     at java.security.AccessController.doPrivileged(Native Method)
    9.     at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    10.     at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    11.     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    12.     at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    13.     at java.lang.Thread.run(Unknown Source)
    14.  
    It seems like it's something to do with initialising the .fmxl file to me but I'm not too sure.
     
  8. Not if there's nothing in the stack trace indicating it...
     
  9. Don't lead your relative paths with a file separator.
    Code (Text):
    1. Resources.getAsStream("com/smitty260/cowkiller/CowKiller.fxml"),
    should be fine.
     
  10. I dunno then lol. I just thought it would have been that because that's the line of the file that the error references.

    Didn't work either. xD
    Don't think the file seperator at the front makes a difference. :s
     
  11. It technically does, to indicate an absolute or relative path from the class which is actually creating the URL.
     
  12. Oh, well there ya go haha. Didn't work though. :(
    --- Double Post Merged, Feb 21, 2016, Original Post Date: Feb 21, 2016 ---
    Okay, turns out I didn't add it to the .XML file as a resource. Derp. That's embarrassing.
     

Share This Page

Loading...