This is a question that has been asked like 100 times on this site, but I have looked at all of them and even though they all were solved, none of the solutions worked for me.
Here's what my code looks like:
public Button1(Client client, String imgName) {
this.client = client;
try {
this.icon = ImageIO.read(this.getClass().getResourceAsStream("/resources/" + imgName));
} catch (IOException e) {
e.printStackTrace();
}
When the code runs it results in the following error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
The string imgName is passed to the constructor from a child class and is the name of an image (e.g. image.png). I also have made sure that my resources folder is in the root of the project folder, and is included as a source folder in the eclipse project. I've also made sure that System.getProperty("user.dir")
points to the correct location. I have also tried using getResource() instead of getResourceAsStream(), but it still does not work.
Try using this:-
this.icon = ImageIO.read(new FileInputStream("res/test.txt"));
where res
folder is present at the same level as your src
folder. Also, if you notice, the slash /
before the res
folder name was removed.