我正在编写游戏并尝试从ImageLoader
类加载和映像,并且我一直收到此错误:
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)
以下是我的类调用ImageLoader
类的代码,但我的catch块也收到错误:
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);
}
以下是我的ImageLoader
课程的代码:
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;
}
}
为什么它会抛出空!我试图将resources/Asteroid.jpg
的路径改为/resources/Asteroid.jpg
但它没有改变任何内容。
编辑:resources
与我的类路径有关,因为它是我存储图像和指向资源的包。