0

スキャナーを使用してタワーディフェンスゲームのレベルを編集しようとしています。ただし、レベル(タイル画像)はカスタムファイルのレベル(0は草1は石-1はなしなど)に更新されません。エラーを見つけましたが、どうすれば修正できますか?これを取り除くには何を追加/変更する必要がありますか?

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

11行目:for(int y=0;y<Window.room.block.length;y++) { 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 () { }
    public void define () { }    
    public void physic () { }   

    public void draw(Graphics g) {

        block = new Block[worldHeight][worldWidth];         

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

ばかげた質問でごめんなさい、私は初心者です。

4

1 に答える 1

1

手っ取り早い解決策は、Window.roomがnullにならないようにテストし、.blockをテストすることです。

        Scanner loadLevelsScanner = new Scanner (loadPath);
        if ((Window.room != null) && 
            (Window.room.block != null)) {
            // ... block until catch block 
        }

私が書いた簡単なTestappは、そうすればこれまでのところ機能します。

ただし、「静的」とは何か、その理由と使用方法を理解する必要があります。よくある初心者の間違いは、コンパイラをサイレントにするためだけに「静的」キーワードを挿入することです。

クラスとその属性を初期化する順序を調べます。

ブロックでは、ウィンドウにアクセスするには、参照が必要です。参照は、ブロックのctorに渡すことができます。

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

    public Block (int x, int y, int width, int height, int groundID, int airID, Window window) {
        setBounds (x, y, width, height);
        this.groundID = groundID;
        this.airID = airID;
        this.window = window;
    }
    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);
        }
    }
}

ブロックを作成するのは誰ですか?それは部屋なので、部屋自体が窓について知る必要があります(基本的にデザインを変更しない限り)。

public Room (Window w) {
    block = new Block [worldHeight] [worldWidth];
    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, w);
        }
    }
}

ブロック配列が作成され、初期化され、ブロックにWindowパラメータが渡されます。

描画では、配列を何度も再作成したり、ブロックを再作成したりすることはありませんが、ブロックを再描画するだけです。

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].draw (g);
        }
    }
}

Windowで、Roomを作成し、それにwindow-referenceを渡します。

public void define () {
    room = new Room (this);
    levels = new Levels ();
于 2012-05-12T15:09:27.307 に答える