在尝试运行以下代码时,它会抛出IllegalArgumentException
public class Receta {
private final StringProperty nombre;
private final StringProperty dificultad;
private final StringProperty tipo;
private final StringProperty plato;
private final StringProperty ingredientes;
private final StringProperty observaciones;
private final StringProperty[] pasos = new StringProperty[10];
private final StringProperty comensales;
private final StringProperty tPrep;
private final StringProperty tCoc;
private final IntegerProperty valoracion;
private HBox caja = new HBox(3);
private ImageView imagen;
private VBox cajaV = new VBox(2);
private HBox cajaDif = new HBox(2);;
public Receta(String n){
nombre = new SimpleStringProperty(n);
dificultad = new SimpleStringProperty("Normal");
tipo = new SimpleStringProperty("Primero");
plato = new SimpleStringProperty("Carne");
ingredientes = new SimpleStringProperty("Ninguno");
observaciones = new SimpleStringProperty("Nada");
comensales = new SimpleStringProperty("2");
tPrep = new SimpleStringProperty("180");
tCoc = new SimpleStringProperty("20");
valoracion = new SimpleIntegerProperty(4);
imagen = new ImageView("/Recetas/src/lupita.png"); //The exception is thrown here
cajaDif.getChildren().addAll(null,new Label("Normal"));
cajaV.getChildren().addAll(cajaDif,new Label(tPrep.get()));
caja.getChildren().addAll(imagen,new Label(n),cajaV);
}
private ImageView imagenDif(){
String d = dificultad.get();
if(d.equals("Facil")) return new ImageView(new Image("../../facil.png"));
if(d.equals("Medio")) return new ImageView(new Image("../../medio.png"));
return new ImageView(new Image("../../dificil.png"));
}
public String getNombre(){
return nombre.get();
}
public void setNombre(String n){
nombre.set(n);
}
public String getDif(){
return dificultad.get();
}
public void setDif(String d){
dificultad.set(d);
}
public String getTipo(){
return tipo.get();
}
public void setTipo(String t){
tipo.set(t);
}
public String getPlato(){
return plato.get();
}
public void setPlato(String p){
plato.set(p);
}
public String getIngr(){
return ingredientes.get();
}
public void setIng(String i){
ingredientes.set(i);
}
public String getObs(){
return observaciones.get();
}
public void setObs(String o){
observaciones.set(o);
}
public String getPaso(int i){
return pasos[i].get();
}
public void setPaso(int i, String p){
pasos[i].set(p);
}
public String getCom(){
return comensales.get();
}
public void setCom(String c){
comensales.set(c);
}
public String getTPrep(){
return tPrep.get();
}
public void setTPrep(String t){
tPrep.set(t);
}
public String getTCoc(){
return tCoc.get();
}
public void setTCoc(String p){
tCoc.set(p);
}
public int getVal(){
return valoracion.get();
}
public void setVal(int v){
valoracion.set(v);
}
public HBox getCaja(){
return caja;
}
我收到以下错误
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class recetas.MainRecetas
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1323468230.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/934330049.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/97828179.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Unknown Source)
at javafx.scene.image.Image.<init>(Unknown Source)
at javafx.scene.image.ImageView.<init>(Unknown Source)
at recetas.model.Receta.<init>(Receta.java:41)
at recetas.MainRecetas.<init>(MainRecetas.java:20)
... 18 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
... 23 more
Exception running application recetas.MainRecetas
主要简单地调用Receta's constructor
然后抛出异常,我已经检查并更改了图像的url
几次,以防它出错但看起来不是这样。
答案 0 :(得分:1)
提供给ImageView的字符串或Image constructor指定URL(包括协议),如果没有协议,则指定类路径位置。
URL支持的所有URL都可以传递给构造函数。如果传递的字符串不是有效的URL,而是路径,则在这种情况下在类路径上搜索Image。
因为您没有提供new ImageView("/Recetas/src/lupita.png")
的协议,所以应用程序正在类路径中查找图像。您的类路径不包含您的源目录。
如果lupita.png位于类路径的根目录下,则可以使用以下命令加载:
new ImageView("/lupita.png");
如果要使用文件协议而不是类路径从文件系统加载lupita,则可以使用绝对路径加载它(假设/ Recatas是当前文件系统根目录下的子目录):
new ImageView("file:///Recatas/src/lupita.png");
不建议从本地文件系统的绝对路径加载以分发您的应用程序,因为该文件可能不会在其他人的系统中位于相同的绝对位置。