1

Here is the method that's giving me trouble:

    private static final String theURL = "/optimize/images/prun0.png";

    public static void createChallenge() {
       Stage challStage = new Stage();
       Group challGroup = new Group();
       Rectangle rect = new Rectangle(900, 500);
       Image image = new Image(theURL);
       System.out.println(image.getHeight()); //Prints 0.0
       rect.setFill(new ImagePattern(image)); //Gives runtime error: "Image must be non-null"
       challStage.setResizable(false);
       challStage.setTitle("Optimize!");
       //ImageView iv = new ImageView(image);
       challGroup.getChildren().add(rect);
       Scene challScene = new Scene(challGroup, 900, 500);
       challStage.setScene(challScene);
       challStage.setMinHeight(500);
       challStage.setMinWidth(900);        
       challStage.show();
       challStage.centerOnScreen();
       challStage.setX(challStage.getX()-150);
   }

As it says, it throws a runtime error about image being null. Am I missing something basic? Also, the ultimate goal here is to put an image onto a Stage or Scene. I tried to do it simply with adding ImageView objects to the defined challGroup, but the Stage came up blank. Due to this, I tried to add the image to a Rectangle object and there found the actual runtime error. I have thrice-checked the URL. I've also tried using "file:prun0.png" to similar results. Any advice is appreciated. Thanks to all.

Jumpman64
  • 11
  • 1
  • See [Where does javafx.scene.image.Image(“flower.png”) look for flower.png?](http://stackoverflow.com/questions/10575410/where-does-javafx-scene-image-imageflower-png-look-for-flower-png), also consider [ImageView](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html) – jewelsea May 15 '14 at 22:29
  • I actually tried copy-pasting verbatim from the ImageView API earlier with a few different images. The screen comes up totally blank for me :( – Jumpman64 May 15 '14 at 22:37
  • Figured it out, chalk it up to a really stupid error. I named the Netbeans project "Optimize!" with an exclamation. When it went to get the file, that character probably tripped it up in the address. Problem solved. Thanks for the earlier answer! – Jumpman64 May 16 '14 at 00:25

0 Answers0