我有一个典型的NullPointerException:
Exception in thread "Thread-0" java.lang.NullPointerException
at com.game.main.SpriteSheet.grabImage(SpriteSheet.java:17)
at com.game.main.Textures.getTextures(Textures.java:30)
at com.game.main.Textures.<init>(Textures.java:21)
at com.game.main.Game.init(Game.java:80)
at com.game.main.Game.run(Game.java:116)
at java.lang.Thread.run(Thread.java:745)
并且我非常确定我知道null的位置,我认为这是这个方法(这也就是getImage方法在我创建的spritesheet.png图像中加载你想要的任何网格)
public BufferedImage grabImage(int column, int row, int width, int height){
System.out.println("img is " + image); //<<<< How I tested for null.. May not be the best way, but image said to be null
BufferedImage img = image.getSubimage((column * 32) - 32, (row * 32) - 32, width, height);
return img;
}
我已经看过这个类中的所有内容,这是类:SpiteSheet.class
package com.game.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image){
this.image = image;
System.out.println("img is " + this.image); // <<<<<<<<< THIS ONE SHOWS "img is nullException in thread "Thread-0" " in the console
}
public BufferedImage grabImage(int column, int row, int width, int height){
System.out.println("img is " + image); //<<<< How I tested for null.. May not be the best way, but image said to be null
BufferedImage img = image.getSubimage((column * 32) - 32, (row * 32) - 32, width, height);
return img;
}
}
我想知道我怎么能启动它?虽然我猜测它已经是image.getSubimage(代码),但我测试了this.image,它说,那就是NullPointerException ...但它应该已经启动了。
可能涉及的更多课程
Textures.class
package com.game.main;
import java.awt.image.BufferedImage;
public class Textures {
public BufferedImage[] player = new BufferedImage[3];
public BufferedImage[] missile = new BufferedImage[3];
public BufferedImage[] enemy = new BufferedImage[3];
private SpriteSheet ss;
private Game game;
public Textures(Game game){
this.game = game;
ss = new SpriteSheet(game.getSpriteSheet());
getTextures(); // This is line 21
}
public void getTextures(){
player[0] = ss.grabImage(1, 1, 32, 32); //This is line 30
player[1] = ss.grabImage(1, 2, 32, 32);
player[2] = ss.grabImage(1, 3, 32, 32);
missile[0] = ss.grabImage(2, 1, 32, 32);
missile[1] = ss.grabImage(2, 2, 32, 32);
missile[2] = ss.grabImage(2, 3, 32, 32);
enemy[0] = ss.grabImage(3, 1, 32, 32);
enemy[1] = ss.grabImage(3, 2, 32, 32);
enemy[2] = ss.grabImage(3, 3, 32, 32);
}
}
game.class非常大,所以我会给出它所涉及的主要方法(得到它?init(),在其中...)
Game.class
public void init(){
requestFocus(); //Brings focus to the screen at the start
BufferedImageLoader loader = new BufferedImageLoader();
try{
spriteSheet = loader.loadImage("/Sprite_Sheet.png");
background = loader.loadImage("/Background.png");
}catch(IOException e){
e.printStackTrace();
}
addKeyListener(new KeyInput(this));
tex = new Textures(this);
p = new Player(300,300, tex);
c = new Controller(tex);
ea = c.getEntityA();
eb = c.getEntityB();
c.createEnemy(enemyCount);
}
如果你需要我展示另一堂课,我会很乐意这样做,因为最大和唯一的大赛(ish)就是游戏
**编辑:**这是getSpriteSheet方法
`public BufferedImage getSpriteSheet(){ return spriteSheet; }`
** EDIT1:**这是BufferedImageLoader类
package com.game.main;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path) throws IOException{
image = ImageIO.read(getClass().getResource(path));
return image;
}
}
** EDIT2:**当我尝试从头开始尝试时,我只是得到了这个错误,我也不认为我在遇到错误时犯了错误。所以病了,保持为/Sprite_Sheet.png;
Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at com.game.main.BufferedImageLoader.loadImage(BufferedImageLoader.java:14)
at com.game.main.Game.init(Game.java:72)
at com.game.main.Game.run(Game.java:117)
at java.lang.Thread.run(Thread.java:745)
** EDIT3:**我已经检查过是否只有spriteSheet加载为null,而且背景并没有给出null
**找到答案:**我格式化了错误,虽然它是PNG,但显然不对,所以我用不同的图像编辑器打开它并再次导出。答案 0 :(得分:0)
Game.spriteSheet
成员在调用ss = new SpriteSheet(game.getSpriteSheet())
时尚未初始化,由tex = new Textures(this)
中的Game.init()
触发。
奇怪的是,在Game.init()
中,spriteSheet
在new Texture(this)
被调用之前被初始化。您可以在spriteSheet
中使用spriteSheet = loader.loadImage("/Sprite_Sheet.png")
的try / catch块后检查Game.init()
是否为空。