Class.getResource() returns null

北慕城南 提交于 2019-11-26 14:43:28

Assuming getClass() returns com.foo.bar.MyActionListener, getClass().getResource("img/foo.jpg") looks for a file named foo.jpg in the package com.foo.bar.img. If the image is not in this package, or if it is in this package but its root directory is not in the classpath, the method will return null.

If the img folder is at the root of the classpath, you should use getClass().getResource("/img/foo.jpg") (note the leading /), or getClass().getClassLoader().getResource("img/foo.jpg").

You should give the relative path for your source file. For exemple if you have this:

src
 --img
 --classes

And you are in the classes folder, you should write this:

getClass().getResource("../img/" +num.getText()+".jpg")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!