抽象类和子类中的静态变量初始化

时间:2016-04-28 14:29:07

标签: java image error-handling static abstract-class

我是错误处理的新手。我在变量初始化方面遇到了麻烦。只要图片存在就可以正常工作,但是当我故意加载不正确的路径时,我会收到以下错误消息。

所以我的猜测是,如果我正确地修复了静态方法的初始化,那么问题就会得到解决。

    Exception in thread "main" java.lang.ExceptionInInitializerError
at Board.placeBishops(Board.java:149)
at Board.createNewBoard(Board.java:64)
at RunGame.main(RunGame.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


    Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at Bishop.<clinit>(Bishop.java:24)


public class Bishop extends Piece{
private static BufferedImage whiteBishopImage;
private static BufferedImage blackBishopImage;

static {
try {
    whiteBishopImage = ImageIO.read(ChessFrame.class.getResource("/resources/icons/bishop_white.png"));
    blackBishopImage = ImageIO.read(ChessFrame.class.getResource("resources/icons/bishop_black.png"));
}
catch (IOException e) {
    e.printStackTrace();
    whiteBishopImage = warningImage;
    blackBishopImage = warningImage;
    RunGame.getLogger().log(Level.WARNING, "Failed to load Bishop image");
}


}
@Override public BufferedImage getImage() {
if (color == PieceColor.WHITE){
    return whiteBishopImage;
}
else return blackBishopImage;
}

这是抽象类作品。

public abstract class Piece {
protected static BufferedImage warningImage;
protected static BufferedImage myImage;


static {
try {
    warningImage = ImageIO.read(ChessFrame.class.getResource("/resources/icons/warning.png"));

} catch (IOException e) {
    e.printStackTrace();
    JOptionPane.showMessageDialog(null, "Failed to load warning image, application will be shutdown");
    RunGame.getLogger().log(Level.WARNING, "Failed to load warning image, application was shutdown");
    System.exit(1);
}
myImage = warningImage;
}   
public BufferedImage getImage(){
return myImage;
}

1 个答案:

答案 0 :(得分:0)

您的代码中的问题是您只捕获初始化程序中的IOException,但是您没有捕获NullPointerException。这可能是因为检查了IOException,而未选中NullPointerException,因此IDE不会告诉您它可能会在此处发生。

背景:不存在的资源上的Class.getResource()将返回null,并且由于您没有检查其结果,因此将null传递给ImageIO.read,这会导致咳嗽。

所以,你有以下选择(如果你想坚持使用初始化器,那就是......)

捕获NullPointerException以及IOException:

try {
    warningImage = ImageIO.read(ChessFrame.class.getResource("/resources/icons/warning.png"));
} catch (IOException | NullPointerException e) {
    e.printStackTrace();
    JOptionPane.showMessageDialog(null, "Failed to load warning image,application will be shutdown");
    RunGame.getLogger().log(Level.WARNING, "Failed to load warning image, application was shutdown");
    System.exit(1);
}

或首先检查getResource的结果:

try {
    URL imageResource = ChessFrame.class.getResource("/resources/icons/warning.png");
    if(imageResource != null) {
        warningImage = ImageIO.read(imageResource);
    } else {
        // do something useful
    }
} catch (IOException  e) {
    e.printStackTrace();
    JOptionPane.showMessageDialog(null, "Failed to load warning image,application will be shutdown");
    RunGame.getLogger().log(Level.WARNING, "Failed to load warning image, application was shutdown");
    System.exit(1);
}