0

更新我找到了解决方案: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)
4

3 回答 3

5
public Main(){
    TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
    DATA_IMAGES.initImages();
    // ...

您需要交换这两行。DATA_IMAGES.GRASS还在null打电话之前initImages()

(说真的,如果您只是在构造函数的开头放置一个断点并逐步执行它,您会很容易理解这一点。)

于 2012-08-05T21:28:19.767 回答
3

DATA_IMAGES.GRASS将其添加到 TileHelper.MAIN_LIST 时为 null。

您只需在添加 GRASS 图像后调用 DATA_IMAGE.initImages() ,因此在添加 GRASS 图像时它为空。

DATA_IMAGES.initImages();
TileHelper.MAIN_LIST.add(new Tile(DATA_IMAGES.GRASS,100,100));
于 2012-08-05T21:29:38.507 回答
0

initImages如果是静态初始化块而不是函数,您可能会更好。因为你需要确保在使用它初始化的变量之前调用它。你检查过是这样的吗?

于 2012-08-05T21:30:45.487 回答