更新,我找到了解决方法: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)
最佳答案
将DATA_IMAGES.GRASS
添加到TileHelper.MAIN_LIST时为null。
添加GRASS图像后,您只是在调用DATA_IMAGE.initImages(),因此GRASS图像在添加时为null。
DATA_IMAGES.initImages();
TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));