0

我正在尝试使用扫描仪来编辑我的塔防游戏的级别。但是,它不会将级别(平铺图像)更新为自定义文件的级别(0 是草 1 是石头 -1 什么都不是,等等)。我发现了错误,但我该如何解决它,我需要添加/更改什么来摆脱它?我解决了第一个错误,但现在我明白了:

java.lang.NullPointerException
    at Levels.loadLevels(Levels.java:13)
    at Window.define(Window.java:28)
    at Window.paintComponent(Window.java:44)

第 13Window.room.block[y][x].groundID = loadLevelsScanner.nextInt(); 行: 第 28levels.loadLevels(new File("levels/level1.level")); 行: 第 44 行:define();

这是扫描仪文件:

import java.io.*;
import java.util.*;

public class Levels {
    public void loadLevels(File loadPath) {
        try {
            Scanner loadLevelsScanner = new Scanner(loadPath);

            while(loadLevelsScanner.hasNext()) {

                for(int y=0;y<Window.room.block.length;y++) {
                    for(int x=0;x<Window.room.block[0].length;x++) {
                        Window.room.block[y][x].groundID = loadLevelsScanner.nextInt();
                    }
                }

                for(int y=0;y<Window.room.block.length;y++) {
                    for(int x=0;x<Window.room.block[0].length;x++) {
                        Window.room.block[y][x].airID = loadLevelsScanner.nextInt();
                    }
                }

            }

            loadLevelsScanner.close();

        } catch(Exception e) {

        }
    }
}

这是窗口文件:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class Window extends JPanel implements Runnable {

    public Thread thread = new Thread(this);

    public static Image[] tileset_ground = new Image[100];
    public static Image[] tileset_air = new Image[100];

    public static int myWidth, myHeight;

    public static boolean isFirst = true;

    public static Room room;
    public static Levels levels;

    public Window() {
        thread.start();
    }

    public void define() {
        room = new Room();
        levels = new Levels();

        levels.loadLevels(new File("levels/level1.level"));

        for(int i=0;i<tileset_ground.length; i++) {
            tileset_ground[i] = new ImageIcon("resources/tileset_ground.png").getImage();
            tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
        }

        for(int i=0;i<tileset_air.length; i++) {
            tileset_air[i] = new ImageIcon("resources/tileset_air.png").getImage();
            tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
        }

    }

    public void paintComponent(Graphics g) {
        if(isFirst) {
            define();

            isFirst = false;
        }

        g.clearRect(0, 0, getWidth(), getHeight());

        room.draw(g);
    }

    public void run() { 
        while(true) {

            if(!isFirst) {
                room.physic();
            }

            repaint();

            try {
                Thread.sleep(1);
            } catch(Exception e) {

            }

        }

    }

}

这是房间文件:

import java.awt.*;

public class Room {
    public int worldWidth = 40;
    public int worldHeight = 20;
    public int blockSize = 32;

    public Block[][] block;

    public Room() {
        block = new Block[worldHeight][worldWidth];
    }

    public void define() {

    }


    public void physic() {

    }   

    public void draw(Graphics g) {

        for(int y=0;y<block.length;y++) {
            for(int x=0;x<block[0].length;x++) {
                block[y][x] = new Block(x * blockSize, y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir);
                block[y][x].draw(g);
            }
        }
    }

}

这是块文件:

import java.awt.*;

public class Block extends Rectangle {
    public int groundID;
    public int airID;

    public Block(int x, int y, int width, int height, int groundID, int airID) {
        setBounds(x, y, width, height);

        this.groundID = groundID;
        this.airID = airID;
    }

    public void draw(Graphics g) {
        g.drawImage(Window.tileset_ground[groundID], x, y, width, height, null);

        if(airID != Value.airAir) {
            g.drawImage(Window.tileset_air[airID], x, y, width, height, null);
        }

    }

}

最后这是扫描仪应该读取的自定义文件:

1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -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 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
4

2 回答 2

2

阅读错误信息:

java.lang.NullPointerException
    at Levels.loadLevels(Levels.java:11)

Levels.java 的第 11 行是

for(int y=0;y<Window.room.block.length;y++) {

所以,要么Window.room为空,要么Window.room.block为空。

于 2012-05-12T16:11:55.073 回答
2

您还没有实例化Room,这部分是导致NPE: Window.room。有两点需要注意:

  1. 将所有属性都作为static成员是个坏主意
  2. 您需要在第一次使用之前static在某处实例化所有(或实例)属性

在您的代码中,您需要确保在define()in方法Window之前调用loadLevels()in 方法Levels

于 2012-05-12T16:12:44.563 回答