gpt4 book ai didi

java - Java中的静态变量未初始化

转载 作者:行者123 更新时间:2023-12-01 22:52:15 24 4
gpt4 key购买 nike

所以我在使用以下代码时遇到了问题。

public class bw {
public static int checked[][];
public static BufferedImage input;

public static void floodfill(int j, int i, int color, int spotColor, int th) throws Exception {
input.setRGB(j, i, color);
}

public static void main(String args[]) throws Exception {
BufferedImage input = ImageIO.read(new File("C:\\Users\\Aditya\\Desktop\\Lena.png"));
checked = new int[input.getHeight()][input.getWidth()];
floodfill(250, 310, 0, input.getRGB(250,310), 35);
}
}

已经从代码中删除了大部分不相关的部分。检查的静态变量工作正常。但我在主函数中初始化的输入变量仍然为空。它给了我来自洪水填充的空指针异常。

最佳答案

您有一个同名的局部变量,在 main 方法中作为局部变量静态属性。因此,您的静态属性不会被初始化,而是局部变量被初始化

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

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