0

I can't seem to use the Image class. My colleague and I are working on a project together and he seems to be able to run all the code fine without any errors. When I try to run the same code, with the src organized the same way, I get an invalid URL or resource not found error. I have had this problem on other projects and it seems like it is specific to my computer or file system. Any help would be much appreciated. Here is a sample of the code.

    public class HomePageGUI extends Application {

//Create the dispenser. Generates 5 Products in an array within the dispenser class.
private final Dispenser dispenser = new Dispenser();
//Get background image and add it to the stack pane
private final Image bgImage = new Image("/Dispenser Design/Background & Button Images/Background.png");
private final Button backButton = new Button("", new ImageView(
      new Image("/Dispenser Design/Background & Button Images/Navigation Buttons/Back Button.png")));
private StackPane backgroundPane = new StackPane();
private Scene scene;

@Override
public void start(Stage primaryStage) {
    primaryStage = homePage(primaryStage);
    primaryStage.show();
}

//Display the home page
private Stage homePage(Stage stage) {

    //IMAGE CREATION
    Image drinksCategoryImage = new Image (
          "/Dispenser Design/Background & Button Images/Starter Page/Drinks Button.png");

    Image gumCategoryImage = new Image (
          "/Dispenser Design/Background & Button Images/Starter Page/Gum Button.png");

    Image sweetsCategoryImage = new Image (
          "/Dispenser Design/Background & Button Images/Starter Page/Sweets Button.png");
    //END IMAGE CREATION

    //Create the buttons with the image displayed on them.
    Button drinksCategoryButton = new Button(
          "", new ImageView(drinksCategoryImage));
    drinksCategoryButton.setOnAction(e -> {
        drinksCategory(stage); });

    Button gumCategoryButton = new Button(
          "", new ImageView(gumCategoryImage));
    gumCategoryButton.setOnAction(e -> {
        gumCategory(stage); });

    Button sweetsCategoryButton = new Button(
          "", new ImageView(sweetsCategoryImage));
    sweetsCategoryButton.setOnAction(e -> {
        sweetsCategory(stage); });
    //END BUTTON CREATION
tjames
  • 41
  • 6
  • Confirm that the image files are on your hard disk and have appropriate permissions. i.e. The images are readable by the user that the program is running with. What is the output of this command? `ls "/Dispenser Design/Background & Button Images/Background.png"`? – Asaph Mar 12 '18 at 03:02
  • 1
    The strings you are passing are fairly clearly not valid urls (they have white space, for one thing). – James_D Mar 12 '18 at 03:14
  • Hello @Asaph thank you for the reply! I checked my file permissions and all is readable. I also made sure that I was running netbeans as administrator. To answer your question that line of code is used to create the background image that is later used in a pane. That part of the code is not shown. – tjames Mar 13 '18 at 01:22
  • Hello @James_D thank you for your response. My colleague is running this exact same piece of code on the same IDE as me and he does not get any errors. Would the whitespace act differently on different computers? – tjames Mar 13 '18 at 01:24
  • Not sure: but it's certainly not a valid URL. It definitely won't work when you bundle this as a jar file, on any computer. – James_D Mar 13 '18 at 01:39
  • @James_D I removed the whitespace with no success. – tjames Mar 13 '18 at 02:04
  • Well, that was just an example; `&` is not valid in a URL either. Perhaps your image file is not being deployed? You really need to show the project layout, and probably the layout of the build folder in order for this question to be answered. – James_D Mar 13 '18 at 02:17

1 Answers1

0

Ok so after many hours of troubleshooting I figured out that I needed to add file: to the image path. I am not sure why my colleagues did not need to add the file: extension to make the images work, but that is how it ended up working for me.

    productsForSale[ 4] = new Gum("Wriggleys", "Big Red", .75, 1, new Image(
      "file:src/DispenserDesign/IndividualProducts/BigRedGum.jpg"));
tjames
  • 41
  • 6
  • This won't work if/when you bundle the application as a jar file, though, because the image won't be in its own file any more. And it really doesn't make sense to reference the *source* directory at runtime; there's no guarantee that will be available. – James_D Mar 13 '18 at 02:18
  • @James_D what do you recommend. If you were going to make an image how would you do it. If you could write out a snippet of code to show me an example that would be awesome. – tjames Mar 13 '18 at 03:03
  • If it's part of the project, you should get the URL as a resource, and then call `toExternalForm()` on the URL (since the Image constructor needs the URL in string form). See, perhaps, https://stackoverflow.com/questions/34755630/javafx-location-is-not-set-error, which is about getting the correct URL for an FXML file, but the idea is the same. – James_D Mar 13 '18 at 03:25