gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:52:43 26 4
gpt4 key购买 nike

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

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

    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;
}

最佳答案

您的代码中的问题是您仅在初始化程序中捕获了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);
}

关于java - 抽象类和子类中的静态变量初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36917688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com