我正在尝试使xpic等于vpic,就像下面的示例一样。当我尝试编译此代码时,出现错误:“本地变量xpic可能尚未初始化”

ImageIcon xpic;
ImageIcon vpic;

    vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
    vpic = xpic;

最佳答案

我认为您输入错误,是因为您的代码设置了vpic变量的引用,然后完全忽略了它的设置,并尝试将其设置为xpic(可能是null引用)。

从本质上讲,您正在执行的操作与此等效:

// both Strings are null
String str1;
String str2;

// assign a String object to str1:
str1 = "Hello";

// but then ignore and in fact discard the String object, and
// re-set str1 to null by assigning it str2
str1 = str2; //????


您可能要更改

vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
vpic = xpic;




vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
xpic = vpic;

10-08 03:13