0
package com.example.checking;

import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HelloApplication  extends Application{

    //Main method to run program
    public static void main(String[] args) {
        //to launch the application
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {

        //Now  we need a scene inside stage and root nodes.
        Group root = new Group();
        Scene scene =  new Scene(root, 600,600, Color.LIGHTBLUE);
       
       //Creating and Displaying icon.
        Image i = new Image("icon.png");
        stage.getIcons().add(i);
        
        Text text = new Text();
        text.setText("Whoaaaa!!!");
        text.setX(50);
        text.setY(50);

        //Adding text to our root node

        text.setFont(Font.font("Verdana", 50));
        text.setFill(Color.LIMEGREEN);
        //Setting title
        Line line =  new Line();
        line.setStartX(200);
        line.setStartY(200);
        line.setEndX(500);
        line.setEndY(200);
        line.setStrokeWidth(5);

        Rectangle rectangle = new Rectangle();
        rectangle.setWidth(100);
        rectangle.setHeight(100);
        rectangle.setX(300);
        rectangle.setY(250);
        rectangle.setStrokeWidth(5);
        rectangle.setFill(Color.BLUE);
        rectangle.setStrokeWidth(5);
        rectangle.setStroke(Color.BLACK);

        Polygon triangle = new Polygon();
        triangle.getPoints().setAll(
                200.0, 200.0,
                300.0, 300.0,
                200.0, 300.0,
                300.0, 200.0
        );

        triangle.setFill(Color.YELLOW);

        Circle circle = new Circle();
        circle.setCenterX(350);
        circle.setCenterY(350);
        circle.setRadius(50);
        circle.setFill(Color.ORANGE);



        root.getChildren().add(text);
        root.getChildren().add(line);
        root.getChildren().add(triangle);
        root.getChildren().add(rectangle);
        root.getChildren().add(circle);


        stage.setTitle("Notes and Password Manager");
        //Setting stage height and width
//        stage.setHeight(750);
//        stage.setWidth(630);
        //Set the scene so it shows in the stage.
        stage.setScene(scene);
        stage.setResizable(false);
        //To show the stage we use .show()
        stage.show();
    }
}


This is my code that I am trying to run, If I comment out my Image i object part, everything will work fine, with no issues, but as soon as I add an Image object it will give following errors

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.graphics@19/javafx.scene.image.Image.validateUrl(Image.java:1138)
    at javafx.graphics@19/javafx.scene.image.Image.<init>(Image.java:628)
    at com.example.checking.HelloApplication.start(HelloApplication.java:76)
    at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.graphics@19/javafx.scene.image.Image.validateUrl(Image.java:1123)
    ... 11 more
Exception running application com.example.checking.HelloApplication

Now possible solution could be to make a module-info.java package and add require javafx.control and javafx.fxml but whenever I add module-info.java it gives me this error : -

Error occurred during initialization of boot layer java.lang.module.FindException: Module Checking not found

So to deal with this, I deleted the module-info.java and instead added this :-

--module-path "C:\Users\jaina\Documents\openjfx-19_windows-x64_bin-sdk (1)\javafx-sdk-19\lib" --add-modules javafx.controls,javafx.fxml

in my VM options and it works just fine for everything except for Image objects, help required, cause I am unable to get to any solutions.

Krattza
  • 21
  • 1
  • 1
  • 4
  • the error message tells you exactly what's wrong: _Invalid URL or resource not found_ :) See the duplicate for learning how to access resources. – kleopatra Oct 28 '22 at 12:08
  • Man, I am such a dumbo, thanks I thought it had something to do with VM. – Krattza Oct 28 '22 at 12:21

0 Answers0