0

I am trying to make a 3D game in java. So I decided to add a class to each material needed so I started with wood. when I added diffuseMap I got InvocationTargetError. Why did this happen? Here is the Wood.java code

Edit: Changing to url or other ways did not work


package com.front.fire.graphics.shapes.Cuboid;

import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;

public class Wood extends Box {
    public Wood () {
        super();

        PhongMaterial mat = new PhongMaterial(Color.LIME);
        mat.setDiffuseMap(new Image("Path to texture"));
        this.setMaterial(mat);

    }
}


Error trace

Exception in Application constructor
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.front.fire.FrontFireMain
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
        at java.base/java.lang.Thread.run(Thread.java:831)       
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78)        
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
        ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
        at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1128)
        at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:618)
        at com.front.fire.graphics.shapes.Cuboid.Wood.<init>(Wood.java:13)
        at com.front.fire.FrontFireMain.<init>(FrontFireMain.java:21)
        ... 14 more
Caused by: java.net.MalformedURLException: unknown protocol: c   
        at java.base/java.net.URL.<init>(URL.java:679)
        at java.base/java.net.URL.<init>(URL.java:568)
        at java.base/java.net.URL.<init>(URL.java:515)
        at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1122)
        ... 17 more
Exception running application com.front.fire.FrontFireMain 

Edit: I added the error track Can someone please find solution for this

Thank you

deateaterOG
  • 111
  • 1
  • 3
  • 9
  • 1
    The `Image` constructor expects a URL (or at least a _resource_ path), but you're likely passing it a file path (e.g. `C:\Users\....`). So it thinks the drive letter is the URL scheme, but that scheme is not known. That said, your image should probably be a resource anyway; check out [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). – Slaw Sep 24 '21 at 05:24
  • It still is not solved – deateaterOG Sep 24 '21 at 05:43
  • Ok, I reopened it for you. – jewelsea Sep 24 '21 at 06:38
  • What is the actual value for "Path to texture"? You should be able to find it using `new Image(Wood.class.getResource("texture.png").toExternalForm())`. For that to work, ensure that “texture.png” is in the same location as `Wood.class` before you run your app. – jewelsea Sep 24 '21 at 06:48
  • It's still showing the same error – deateaterOG Sep 24 '21 at 06:53
  • You tried all suggested approaches and troubleshooting recommendations from the comments and the duplicate, and for every single one you receive an error which says, “unknown protocol: c”? That seems quite strange, at minimum I think there would be a different cause for the exception when different approaches are used. Try using a http url to retrieve an image off the web and see if that works for you at least it should give you a different error. – jewelsea Sep 24 '21 at 08:53
  • For example: [https://www.myfreetextures.com/wp-content/uploads/2014/10/seamless-wood-background-1.jpg](https://www.myfreetextures.com/wp-content/uploads/2014/10/seamless-wood-background-1.jpg). – jewelsea Sep 24 '21 at 09:00

0 Answers0