Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:枚举中的异常?_Java_Image_Exception_Enums - Fatal编程技术网

Java:枚举中的异常?

Java:枚举中的异常?,java,image,exception,enums,Java,Image,Exception,Enums,我试图加载一个图像作为枚举的一部分,但它引发了一个异常,我找不到用try-catch对其进行排序的方法,是否有其他方法,或者不可能 下面是一些注释代码,我使用的是Slick2d库 public enum Material { //An enum of different materials, with different propierties and images GRASS (5, false, null, true), //Here I try to load an Image

我试图加载一个图像作为枚举的一部分,但它引发了一个异常,我找不到用try-catch对其进行排序的方法,是否有其他方法,或者不可能

下面是一些注释代码,我使用的是Slick2d库

    public enum Material {
//An enum of different materials, with different propierties and images

GRASS (5, false, null, true),

//Here I try to load an Image, but loading images throw an Exception, a Slick Exception I tried surronding it with try catch but its not working.
DIRT (5, false, new Image("res/Dirt.png"), true), 

WOOD (10, false, null, false), 

SNOW (1, false, null, false), 

ICE (1, false, null, false), 

LEAVES (1, false, null, false), 

STONE(15, false, null, false), 

COBBLESTONE(10, false, null, false), 

LOG(15, false, null, false),

AIR(1, false, null, false);


///variables
private int Resis;
private boolean traspasable;
private static Image I;
private boolean TreesCanGrow;

//getters
public int getResis(){
    return this.Resis;
}

public Image getTile(){
    return this.I;
}

public boolean isTraspasable(){

return this.traspasable;

}

public boolean getTreesCanGrow(){

return this.TreesCanGrow;

}


///constructor
private Material(int ExplosionResis, boolean isTraspasable, Image I, boolean TCG){  
this.Resis = ExplosionResis;
this.traspasable = isTraspasable;
this.TreesCanGrow = TCG;    
}


}
编辑: 新承包商

///constructor
try {
    private Material(int ExplosionResis, boolean isTraspasable, String image, boolean TCG){ 
        this.Resis = ExplosionResis;
        this.traspasable = isTraspasable;
        this.TreesCanGrow = TCG;    
        this.I = new Image(image);
    }
}
catch (SlickException e) {
    e.printStackTrace();
}

}

我不建议将可能失败的代码放入枚举构造函数或作为枚举值声明的参数。如果代码引发异常,枚举类可能不会加载

您可以尝试以下方法:

/** An enum of different materials, with different properties and images. */
public enum Material {
  GRASS (5, false, null, true),

  DIRT (5, false, "res/Dirt.png", true), 

  ...

  AIR(1, false, null, false);


  private final int resis;
  private final boolean traspasable;
  private final String imagePath;
  private Image image;
  private final boolean treesCanGrow;

  private Material(int resis, boolean traspasable, String imagePath,
      boolean treesCanGrow) {  
    this.resis = resis;
    this.traspasable = traspasable;
    this.imagePath = imagePath;
    this.treesCanGrow = treesCanGrow;    
  }

  public synchronized Image getTile() throws SlickException {
    if (image == null && imagePath != null) {
      image = new Image(imagePath);
    }
    return image;
  }
}

然而,就个人而言,我更喜欢enum是不可变的,因此如果它是我的代码,我会将图像的创建放在其他地方(可能在延迟加载的
映射中)。

是否要共享一些代码?能否显示异常的代码和堆栈跟踪?如果不显示一些代码,我们帮不了你。就我个人而言,我觉得这是一个比@BeshGurung的解决方案更好、更干净的解决方案。我不喜欢构造函数中的try/catch,但我确实应该像NamshubWriter所说的那样使用map。如果你的代码有效,你可以随时让它检查,也许你会得到更多的提示