我一直在尋找空指針幾個小時,感覺就像我一直在浪費盯着同一屏幕。Slick2d地圖NullPointerException
所以,我試圖打開一個SpriteSheet並從該表中獲取圖像,不幸的是,在我最好的嘗試中它返回一個錯誤。
我相信地圖是問題;我有一個地圖,用於記錄精靈表的標題和目錄。當我嘗試訪問指定的SpriteSheet時,它返回一個NullPointerException。
不管怎麼說,得到的代碼:
package main;
import java.util.HashMap;
import java.util.Map;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
public class Resources {
public static Map<String, SpriteSheet> sprites;
public static Map<String, Image> images;
public Resources(){
sprites = new HashMap<String, SpriteSheet>();
images = new HashMap<String, Image>();
try {
sprites.put("Tiles", getSpriteSheet("Tiles.png", 32, 32));
} catch (SlickException e) {
e.printStackTrace();
}
}
private static SpriteSheet getSpriteSheet(String image, int w, int h) throws SlickException{
return new SpriteSheet("/res/" + image, w, h);
}
public static Image getImage(String image, int x, int y) throws SlickException{
Image img = sprites.get("Tiles").getSubImage(x, y);
if(img != null){
return img;
}else{
System.out.println("ERROR LOADING IMAGE");
return null;
}
}
}
請編輯您的問題,並發表stracktrace –