简体   繁体   中英

Static variable initialization in abstract class and subclass

I am new to error handling. I am having trouble with variable initialisation. It works fine as long as the pictures exists, but when I deliberately load an incorrect path, I receive the following error message below.

So my guess is if I fix the Initialization of the static methods correctly then the issue will get solved.

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

This is the abstract class piece.

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

The problem in your code is that you are catching only the IOException in your initializers, but you do NOT catch the NullPointerException. This is probably due to the fact, that the IOException is checked whereas the NullPointerException is unchecked, so that your IDE does not tell you that it might occur here.

Background: Class.getResource() on a non-existing resource will return null, and as you do not check its result, you pass null to ImageIO.read, which coughs up.

So, you have the following options (if you want to stick to initializers, that is...)

Either catch NullPointerException along with the 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);
}

or check the result of getResource first:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM