问题
I'm currently programming a game and trying to load and image from an ImageLoader
class and I keep receiving this error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1338)
at game.ImageLoader.getImage(ImageLoader.java:24)
at game.TestPanel.<init>(TestPanel.java:38)
at game.TestApplication.main(TestApplication.java:26)
Here is the code to my class that's calling the ImageLoader
class, yet I also receive an error on my catch block:
public TestPanel ()
{
try
{
ClassLoader myLoader = this.getClass().getClassLoader();
ImageLoader loader = new ImageLoader();
backdrop = loader.getImage("resources/Asteroid.jpg");
ImageLoader test = new ImageLoader();
ship = test.getImage("resources/Alien-Ship.png");
InputStream pointStream = myLoader.getResourceAsStream("resources/path_1.txt");
Scanner s = new Scanner (pointStream);
objectPath = new PathPoints(s);
squid = objectPath.getStart();
}
catch (IOException e)
{
System.out.println ("Could not load: " + e);
}
addMouseListener(this);
}
And here is the code for my ImageLoader
class:
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class ImageLoader {
public Image loadedImage;
public ImageLoader(){
}
public Image getImage(String s){
try
{
ClassLoader myLoader = this.getClass().getClassLoader();
InputStream imageStream = myLoader.getResourceAsStream(s);
loadedImage = ImageIO.read(imageStream);
}
catch (IOException e)
{
System.out.println ("Could not load: " + e);
}
return loadedImage;
}
}
Why is it throwing null!, I have tried to change the path of resources/Asteroid.jpg
to /resources/Asteroid.jpg
yet it changed nothing.
Edit: resources
relates to my classpath because it is the package in which I am storing my image and point resources in.
来源:https://stackoverflow.com/questions/15802234/illegal-argument-exception-for-loading-image