我目前正在编写游戏并尝试从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;
}
}
为什么它会抛出 null!,我试图改变 to 的路径,resources/Asteroid.jpg
但/resources/Asteroid.jpg
它什么也没改变。
编辑:resources
与我的类路径有关,因为它是我存储图像和点资源的包。