I am making a game in java which involves drawing images to a frame. When I attempt to draw the images, I get the following error:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at main.Game.<init>(Game.java:57)
at main.Game.main(Game.java:319)
Why am I getting this? Here is the code for the area I am accessing the files:
try {
playerImage = ImageIO.read(this.getClass().getResourceAsStream("resources/player.png"));
bulletImage = ImageIO.read(this.getClass().getResourceAsStream("resources/bullet.png"));
enemyImage = ImageIO.read(this.getClass().getResourceAsStream("resources/enemy.png"));
highScoreReader = new BufferedReader(new FileReader("/files/HIGH_SCORE.txt"));
highScoreWriter = new BufferedWriter(new FileWriter("/files/HIGH_SCORE.txt"));
} catch (Exception e) {
e.printStackTrace();
}
Here is a picture of the file directories:
Am I coding the directory wrong? Am I not grabbing the image correctly?
Is the "s", you have "resour'C'es" and the folder is "resour'S'es"
Your resource folder is named resourses but your code is calling from "resources/player.png".
Related
I am making a program where the user chooses an image file on their computer and it displays it in a window. I have the line of code
image = ImageIO.read(getClass().getResourceAsStream(path));
where path is the absolute path to the image. The error it returns is
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
What am I doing wrong or is it not possible to get an Image outside of the project file?
So, getClass().getResourceAsStream() reads the image from default class path(either in the folder of your classes or the contents of the jar file)!
So you would want :
BufferedImage img = null;
try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
}
I've looked around and have tried many fixes, but I can't seem to find one that works.
I'm making a simple frogger game, and I originally had all of my sprites loaded in the project directory like img/spritename.png:
image = ImageIO.read(getClass().getResourceAsStream("img/map.png"));
or
ImageIO.read(getClass().getResource(sprite));
Everything worked fine and dandy, but I went to move all of my images to an external directory so I can create an installer for it.
"C:\Program Files\DIR\img\" and all of the sprites no longer load.
The code I thought would fix it but didn't
BufferedImage image = null;
File dir = new File(Config.imgDir + "map.png");
try {
image = ImageIO.read(dir);
} catch (IOException e) {
e.printStackTrace();
}
if (image == null) {
return;
}
}
Config code:
public static String imgDir = "C:/Program Files/EmulousSoft/Frogger/img/";
Does anyone have any ideas? I'm sure it's simple, but every time I try different methods, I get "Value cannot be a null", "cannot find directory" or this.
I am making a space shooter in Java, but when I try to load up the image resources, I get a null pointer exception. Everything works fine except the images. Am I coding the directory wrong? How can I fix it?
Here is my code:
BufferedReader highScoreReader;
BufferedWriter highScoreWriter;
try {
playerImage = ImageIO.read(this.getClass().getResourceAsStream("src/res/player.png"));
bulletImage = ImageIO.read(this.getClass().getResourceAsStream("src/res/bullet.png"));
enemyImage = ImageIO.read(this.getClass().getResourceAsStream("src/res/enemy.png"));
highScoreReader = new BufferedReader(new FileReader("/files/HIGH_SCORE.txt"));
highScoreWriter = new BufferedWriter(new FileWriter("/files/HIGH_SCORE.txt"));
} catch (Exception e) {
e.printStackTrace();
}
Here is a screenshot of my file directories:
Most probably, you need to copy your images into the build directory of your project. If you want them treated as classpath resources, which it seems you do, make sure they're in a source folder in eclipse (or, if you use maven or similar, in the src/main/resources folder. The point is, they need to be copied to the place where the .class file lives when it's running.
Remember: class.getResourceAsStream(...) returns things from the classpath not from your source path.
I've never seen it done that way.
Try this
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("src/res/player.png"));
catch(IOException e) {
}
or
try {
playerImage = ImageIO.read(new File("src/res/player.png"));
catch (IOException e) {
}
Arnav Garg has discovered the problem.
When your code says something like:
file("src/res/player.png")
the file is not there, i.e.
file.exists()
will return false
Find out where java thinks the file is.
try using
file.getAbsolutePath()
and compare that to your directory structure.
Currently I have code like this in my program:
BufferedImage ReadPicture = null;
try {
ReadPicture = ImageIO.read(new File("C:/Users/John/Documents/NetBeansProjects/Program5/build/classes/Program5/Pictures/TestPicture.png"));
} catch (IOException e) {
}
If I compile my file to a jar and give it to someone else, the program does not work as the classpath is specific to my computer. How can I change how I access files/images so that it works on all computers?
For ImageIO in particular, if you always want to read an image from the classpath, without regard to what the classpath actually is, then you can do this:
BufferedImage readPicture = null;
URL imageUrl = getClass().getClassLoader().getResource(
"/Program5/files/Pictures/TestPicture.png");
// Or
// InputStream imageStream = getClass().getClassLoader().getResourceAsStream(
// "/Program5/files/Pictures/TestPicture.png");
// null if not found
try {
readPicture = ImageIO.read(imageUrl);
// null if the image format is unrecognized
} catch (IOException e) {
// ...
}
That relies on the fact that ImageIO can obtain images via URLs. This approach can be used even if the image is packaged in a Jar file, along side your classes (or not).
You can add a folder in your project named files or anything you want.You can make sub-directories in it and arrange files in that.They will be available when you will share it with others.In the code below,"." represents working directory.So make sure the directory structure you are providing,is correct.Try something like this.
BufferedImage ReadPicture = null;
try {
ReadPicture = ImageIO.read(new File("./files/Pictures/TestPicture.png"));
} catch (IOException e) {
}
See Also
Java Project Folder Structure
I'm using a simple way to get my resources for the project. I'm using Eclipse, and I have a 'res' folder to hold the needed files. This is how I load stuff, for example a 'puppy.png' just in my res folder (no subfolders):
String path = "/puppy.png";
try {
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
} catch(Exception ex) { ex.printStackTrace(); }
And sometimes I get an input==null error, and sometiomes not! Not like this time puppy.png loaded but next time it won't. For some classes it always loads correctly, and for the other classes I always get this error. Can anyone explain why can this happen, and how can I fix it, but still use the getResourceAsStream() method?
Please have a look at How to retrieve image from project folder?.
I have mentioned no of ways to read image from different paths.
You can try any one
// Read from same package
ImageIO.read(getClass().getResourceAsStream("c.png"));
// Read from absolute path
ImageIO.read(new File("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.png"));
// Read from images folder parallel to src in your project
ImageIO.read(new File("images\\c.jpg"));
In your case the image must be in the same package where is the class and don't prefix /.
Note that if the resource returns null (meaning it doesn't exist), you will get this error.
Check the input returned like so:
String path = "/puppy.png";
try {
InputStream is = getClass().getResourceAsStream(path);
if (is == null) {
//resource doesn't exist
} else {
BufferedImage image = ImageIO.read(is);
}
} catch(Exception ex) { ex.printStackTrace(); }
Note that you most likely should be using String path = "puppy.png", seeing as you will already be in the content of the project folder.