在Netbeans中为Java应用程序设置ImageIcon会返回NullPointerException

时间:2018-06-28 17:31:04

标签: java netbeans nullpointerexception

我正在尝试为Java应用程序设置ImageIcon,但是当我尝试获取资源时,会收到NullPointerException。

这是Netbeans内部的目录结构:

  • 项目名称
    • 资源 -图片 icon.png
    • src
    • 目标

这是我用来设置ImageIcon的代码:

set.seed(12)
xd <- data.frame(id = sort(sample(3,20, rep=TRUE)), y = rnorm(20))

fxd <- xd[rep(which(xd$id %in% c(1,2)), each = 2),]

fxd
#      id           y
# 1     1 -0.77771958
# 1.1   1 -0.77771958
# 2     1 -1.29388230
# 2.1   1 -1.29388230
# 3     1 -0.77956651
# 3.1   1 -0.77956651
# 4     1  0.01195176
# 4.1   1  0.01195176
# 5     1 -0.15241624
# 5.1   1 -0.15241624
# 6     1 -0.70346425
# 6.1   1 -0.70346425
# 7     1  1.18887916
# 7.1   1  1.18887916
# 8     1  0.34051227
# 8.1   1  0.34051227

我在Netbeans中为iconImage属性插入了以下自定义代码:

protected ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        LOG.error("Couldn't find file: " + path);
        return null;
    }
}

非常感谢您的帮助。

0 个答案:

没有答案
相关问题