更新我找到了解决方案:D感谢您的回复,但没有一个是真正的问题。我的问题是我在将它们添加到ArrayList后初始化图像。
所以我有一个名为Tile的课程:
public class Tile {
BufferedImage img;
int x,y;
public Tile(BufferedImage image, int x, int y)
{
this.x = x;
this.y = y;
this.img = image;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public BufferedImage getImage(){
return img;
}
}
然后我有一个包含图像变量的类,名为DATA_IMAGES:
public class DATA_IMAGES {
//terrain
public static BufferedImage SNOW;
public static BufferedImage GRASS;
public static BufferedImage CAVE;
//Entities
public static BufferedImage TREE;
public static BufferedImage player;
public static int frame = 1;
public static void initImages(){
try {
player = ImageIO.read(new File("res/images/player/f1.png"));
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"Can't Load Images "+e.getMessage(),"Fatal Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
try{
//SNOW = new ImageIcon("res/images/snow.png").getImage();
//CAVE = new ImageIcon("res/images/snow.png").getImage();
GRASS = ImageIO.read(new File("res/images/grass.png"));
TREE = ImageIO.read(new File("res/images/tree.png"));
}catch(Exception ex){
JOptionPane.showMessageDialog(null,"Can't Load Images","Fatal Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
所以当我测试这个时,我有一个名为Main的类:
public class Main extends JFrame {
private static final long serialVersionUID = 6149918602875295087L;
public static void main(String args[]){
new Main();
}
TileHelper tiles = new TileHelper(TileHelper.MAIN_LIST);
public Main(){
TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
DATA_IMAGES.initImages();
setSize(640,480);
setResizable(false);
setTitle("Stranded");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
createBufferStrategy(2);
GameLoop();
}
public void GameLoop(){
Thread gameloop = new Thread(){
public void run(){
while(true){
RenderInit();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
gameloop.start();
}
public void RenderInit(){
BufferStrategy buff = getBufferStrategy();
Graphics g = buff.getDrawGraphics();
g.clearRect(0, 0, getWidth(), getHeight());
g.drawString("COOL TEXT", 100, 100);
Render(g);
g.dispose();
buff.show();
Toolkit.getDefaultToolkit().sync();
}
public void Render(Graphics g){
tiles.RenderAll(g);
}
}
然后你可能会注意到还有一个TileHelper类:
public class TileHelper {
ArrayList<Tile> list;
//Main list for adding tiles
public static ArrayList<Tile> MAIN_LIST = new ArrayList<Tile>();
public TileHelper(ArrayList<Tile> list){
this.list = list;
}
public void RenderAll(Graphics g){
for(int i = 0; i < list.size(); i++){
Tile t = new Tile(list.get(i).getImage(), list.get(i).getX(),list.get(i).getY());
g.drawImage(list.get(i).getImage(),t.getX(),t.getY(),null);
System.out.println(t.getX()+" "+t.getY() + list.get(i).getImage().getHeight());
}
}
}
仔细观察后,我会看到我打印出x值,y值和图像高度。我一直得到一个空指针,因为BufferedImage由于某种原因没有设置?
问题如何修复此问题,以便识别BufferedImage,并且我没有得到空指针。
这是错误,只是你需要它......
Exception in thread "Thread-3" java.lang.NullPointerException
at com.survivalpixel.stranded.TileHelper.RenderAll(TileHelper.java:19)
at com.survivalpixel.stranded.Main.Render(Main.java:60)
at com.survivalpixel.stranded.Main.RenderInit(Main.java:53)
at com.survivalpixel.stranded.Main$1.run(Main.java:35)
答案 0 :(得分:5)
public Main(){
TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
DATA_IMAGES.initImages();
// ...
你需要交换这两行。在致电DATA_IMAGES.GRASS
之前,null
仍为initImages()
。
(说真的,如果您只是在构造函数的开头放置了一个断点并逐步完成它,那么你很容易想到这一点。)
答案 1 :(得分:3)
DATA_IMAGES.GRASS
为null。
添加GRASS图像后,您只是调用DATA_IMAGE.initImages(),因此GRASS图像在添加时为空。
DATA_IMAGES.initImages();
TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
答案 2 :(得分:0)
如果initImages
是静态初始化块而不是函数,那么你可能会更好。因为它是你需要确保在使用它初始化的变量之前调用它。你检查过那种情况吗?