javaarraylist似乎在init方法之后为空

xu3bshqb  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(258)

本质上,在init方法中,我创建了一个数组列表,并用 wizardObjects . wizardobjects有4个适当填充的构造函数。在我运行for循环之后,我甚至有一个 System.out.println 验证列表是否已填写。
我的问题是,在render方法、update方法或任何其他方法启动时,arraylist抛出一个空指针异常。我不会在任何地方重新分配或重新初始化它。
顺便说一句,我用的是slick2d,但我不认为这对这个问题有多重要。

public void init(GameContainer container) throws SlickException {

        SpriteSheet spritesheet1 = new SpriteSheet("/res/mainsprites.png", 32, 32);

        wizardSprite = spritesheet1.getSprite( 5,3 );

        wizardXArray = new int[100];
        wizardYArray = new int[100];

        random = new Random();
map = new TiledMap("/res/map.tmx");

        ArrayList<WizardObject> wizList = new ArrayList <WizardObject>(100);

        for(int i = 0; i < 100 ;i++){

            int randX = random.nextInt(100) * 32;
            wizardXArray[i] = randX;
            int randY = random.nextInt(100) * 32;
            wizardYArray[i] = randY;
            WizardObject wizardObject  = new WizardObject(wizardXArray[i], wizardYArray[i], wizardSprite, map);
            wizList.add(wizardObject);

        }
        for(int x = 0; x < 100 ;x++){
            System.out.println(wizList.get(x));
            System.out.println(wizList.get(x).getX() + "  " + wizList.get(x).getY());

            }

public void render(GameContainer container, Graphics g)
            throws SlickException {

if(wizList == null ){

                    System.out.println("true");
                }}

这就是代码的要点,对于那些没有时间回顾所有事情的人来说。对于那些这样做的人:

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.particles.ParticleSystem;
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.gui.TextField;

import com.tinyline.tiny2d.i;

public class MainGameLoop extends BasicGame {
    private static final int PLAYER_SIZE = 32;
    private static final int TILE_SIZE = 32;
      private boolean[][] blocked;
    private int playerX = 50;
    private int playerY = 50;

    private int widthInTiles;
    private int heightInTiles;

    private int topOffsetInTiles;
    private int [] wizardXArray;
    private int [] wizardYArray;

    private int leftOffsetInTiles;
    private int randX;
    private int randY;
    private TiledMap map;
    private Image wizardSprite;
    private long currentTime;
    private long dblUpdateTime;
    private long updateTime;
    private boolean isTicking = false;
    private boolean isTocking = false;
    private Random random;
    private boolean isIntroOver = false;
    private Image titleImage;
    private Image[] circleImage;
    private Animation circleAnim;
    private SpriteSheet spritesheet1;
    public int[] circleDuration = { 300, 300, 300 };

    private int x = 100;
    private int y = 100;
    private int dx = 1;
    private int dy = 1;

    private int rx = 1;
    private int ry = 450;
    private int rdx = 1;
    private int rdy = 1;

    private int fx = 1;
    private int fy = 450;
    private int fdx = 1;
    private int fdy = 1;

    private int pdx = 1;
    private int pdy = 1;
    private int px = 1;
    private int py = 450;

    private int wdx = 1;
    private int wdy = 1;
    private int wx = 1;
    private int wy = 450;

    private Image image;

    public Image baseBGImage;
    public Image basePotionImage;
    public Image[] brenImage;
    public Animation brenAnim;
    public int[] duration = { 300, 300, 300, 300 };
    public WizardObject wizardObject;
    public List<WizardObject> wizList;

    private boolean isWizardDrawn;

    public MainGameLoop() {
        super("MainGameLoop");
    }

    /**
     * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
     */
    public void init(GameContainer container) throws SlickException {

        SpriteSheet spritesheet1 = new SpriteSheet("/res/mainsprites.png", 32, 32);

        wizardSprite = spritesheet1.getSprite( 5,3 );

        wizardXArray = new int[100];
        wizardYArray = new int[100];

        random = new Random();
        image = new Image("/res/GreenFire.png");
        map = new TiledMap("/res/map.tmx");

        ArrayList<WizardObject> wizList = new ArrayList <WizardObject>(100);

        for(int i = 0; i < 100 ;i++){

            int randX = random.nextInt(100) * 32;
            wizardXArray[i] = randX;
            int randY = random.nextInt(100) * 32;
            wizardYArray[i] = randY;
            WizardObject wizardObject  = new WizardObject(wizardXArray[i], wizardYArray[i], wizardSprite, map);
            wizList.add(wizardObject);

        }
        for(int x = 0; x < 100 ;x++){
            System.out.println(wizList.get(x));
            System.out.println(wizList.get(x).getX() + "  " + wizList.get(x).getY());

            }

        Image[] brenImage = { new Image("/res/Bren1.png"),
                new Image("/res/Bren2.png"), new Image("/res/Bren4.png"),
                new Image("/res/Bren7.png") };
        brenAnim = new Animation(brenImage, duration, true);

         // build a collision map based on tile properties in the TileD map
        blocked = new boolean[map.getWidth()][map.getHeight()];
        for (int x = 0; x < map.getWidth(); x++) {
            for (int y = 0; y < map.getHeight(); y++) {
                int tileID = map.getTileId(x, y, 0);
                String value = map.getTileProperty(tileID, "blocked", "false");
                if ("true".equals(value)) {
                    blocked[x][y] = true;
                }
            }
        }

        updateTime = System.currentTimeMillis() + 200;
        dblUpdateTime = System.currentTimeMillis() + 400;

        titleImage = new Image("/res/title.png");
        baseBGImage = new Image("/res/baseBG.png");
        basePotionImage = new Image("/res/potions.png");
        Image[] circleImage = { new Image("/res/circle_f1.png"),
                new Image("/res/circle_f2.png"),
                new Image("/res/circle_f3.png") };
        circleAnim = new Animation(circleImage, circleDuration, true);

        // calculate some layout values for rendering the tilemap. How many
        // tiles
        // do we need to render to fill the screen in each dimension and how far
        // is
        // it from the center of the screen
        widthInTiles = container.getWidth() / TILE_SIZE;
        heightInTiles = container.getHeight() / TILE_SIZE;
        topOffsetInTiles = heightInTiles / 2;
        leftOffsetInTiles = widthInTiles / 2;

        // system = new ParticleSystem();

        // update the vector of movement based on the initial angle

        Log.info("Window Dimensions in Tiles: " + widthInTiles + "x"
                + heightInTiles);
    }

    /**
     * @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer,
     *      int)
     */
    public void update(GameContainer container, int delta)
            throws SlickException {

        if (rx > container.getWidth() - brenAnim.getWidth())
            rdx = -1;

        if (rx < 0)
            rdx = 1;

        if (ry > (container.getWidth() - brenAnim.getHeight()) - 250)
            rdy = -1;

        if (ry < 440)
            rdy = 1;

        rx = rx + rdx * 3;
        ry = ry + rdy;

        if (fx > container.getWidth() - 1000)
            fdx = -1;

        if (fx < -500)
            fdx = 1;
        if (fy > (container.getWidth() - baseBGImage.getHeight()) - 200)
            fdy = 0;

        if (fy < 450)
            fdy = 1;

        fx = fx + fdx * 2;
        fy = fy + fdy * 2;

        if (px > container.getWidth() - 900)
            pdx = -1;

        if (px < -200)
            pdx = 1;
        if (py > container.getWidth() - wizardSprite.getHeight())
            pdy = -1;

        if (py < 430)
            pdy = 1;

        px = px + pdx;
        py = py + pdy;

        if (wx > map.getWidth())
            wdx = -1;

        if (wx < 0)
            wdx = 1;

        if (wy > map.getWidth() - wizardSprite.getHeight())
            wdy = -1;

        if (wy < 430)
            wdy = 1;

        wx = wx + wdx;
        wy = wy + wdy;

        currentTime = System.currentTimeMillis();
        if (currentTime >= updateTime) {

            isTicking = true;
            updateTime = (currentTime + 200);

        } else {

            isTicking = false;
        }
        if (currentTime >= dblUpdateTime) {

            isTocking = true;
            dblUpdateTime = (currentTime + 400);
            //System.out.println(container.getFPS());

        } else {

            isTocking = false;
        }

        if (isIntroOver == false) {

            isWizardDrawn = false;

            if (x > container.getWidth() - titleImage.getWidth())
                dx = -1;

            if (x < 0)
                dx = 1;

            if (y > (container.getWidth() - titleImage.getHeight()) - 350)
                dy = -1;

            if (y < 0)
                dy = 1;

            x = x + dx;
            y = y + dy;

            if (isTicking == true) {
                // System.out.println("x = " + x);
                // System.out.println("y = " + y);

                for (int x = 0; x < map.getWidth(); x++) {
                    for (int y = 0; y < map.getHeight(); y++) {

                        map.setTileId(x, y, 0, random.nextInt(191));

                    }
                }

                if (container.getInput().isKeyPressed(Input.KEY_ENTER)) {

                    isIntroOver = true;
                    for (int x = 0; x < map.getWidth(); x++) {
                        for (int y = 0; y < map.getHeight(); y++) {

                            map.setTileId(x, y, 0, random.nextInt(191));
                        }

                    }

                }
            }
        }

        if (isIntroOver == true) {

            if(container.getInput().isKeyPressed(Input.KEY_ENTER)){

                isWizardDrawn = true;
            }

            if (container.getInput().isKeyDown(Input.KEY_W)) {

                if (container.getInput().getMouseX() > 775) {
                    if (isTicking == true) {
                        playerX += 2;
                    }

                }
                if (container.getInput().getMouseX() < 50) {
                    if (isTicking == true) {
                        playerX -= 2;
                    }

                }
                if (container.getInput().getMouseY() < 425) {
                    if (isTicking == true) {
                        playerY -= 2;
                    }

                }
                if (container.getInput().getMouseY() > 50) {
                    if (isTicking == true) {
                        playerY += 2;
                    }

                }
            }

            if (container.getInput().isMousePressed(0)) {
                int mx = container.getInput().getMouseX();
                int my = container.getInput().getMouseY();

                // int tileIDmid = map.getTileId(mx, my, 0);
                int px = playerX;
                int py = playerY;
                //
                // map.setTileId((px + (mx / 32) - 13) , py + ((my / 32) - 10),
                // 0, 3);
                int clickedTile = map.getTileId((px + (mx / 32) - 13), py
                        + ((my / 32) - 10), 0);
                if (clickedTile > 185) {

                    System.out.println("true");

                }
                //
                // System.out.println("Clicked x: " + mx + " y: " + my);

            }
        }

        if (container.getInput().isKeyDown(Input.KEY_ESCAPE)) {

            if (container.isFullscreen()) {

                container.setFullscreen(false);
                container.setMouseGrabbed(false);
            } else {

                container.exit();
            }
        }
        if (container.getInput().isKeyDown(Input.KEY_M)) {

            container.setFullscreen(true);
            container.setMouseGrabbed(true);

        }

    }

    private boolean blocked(float x, float y) {

        return blocked[(int) x][(int) y];

    }

    private boolean tryMove(int x, int y) {
        int newx = playerX + x;
        int newy = playerY + y;
// first we try the real move, if that doesn't work
// we try moving on just one of the axis (X and then Y) 
// this allows us to slide against edges
        if (blocked(newx, newy)) {
            if (blocked(newx, playerY)) {
                if (blocked(playerX, newy)) {
// can't move at all!
                    return false;
                } else {
                    playerY = newy;
                    return true;
                }
            } else {
                playerX = newx;
                return true;
            }
        } else {
            playerX = newx;
            playerY = newy;
            return true;
        }
    }

    /**
     * @see org.newdawn.slick.Game#render(org.newdawn.slick.GameContainer,
     *      org.newdawn.slick.Graphics)
     */
    public void render(GameContainer container, Graphics g)
            throws SlickException {

        // draw the appropriate section of the tilemap based on the centre
        // (hence the -(TANK_SIZE/2)) of
        // the player
        int playerTileX = (int) playerX;
        int playerTileY = (int) playerY;

        // caculate the offset of the player from the edge of the tile. As the
        // player moves around this
        // varies and this tells us how far to offset the tile based rendering
        // to give the smooth
        // motion of scrolling
        int playerTileOffsetX = (int) ((playerTileX - playerX) * TILE_SIZE);
        int playerTileOffsetY = (int) ((playerTileY - playerY) * TILE_SIZE);

        // render the section of the map that should be visible. Notice the -1
        // and +3 which renders
        // a little extra map around the edge of the screen to cope with tiles
        // scrolling on and off
        // the screen
        map.render(playerTileOffsetX - (PLAYER_SIZE / 2), playerTileOffsetY
                - (PLAYER_SIZE / 2), playerTileX - leftOffsetInTiles - 1,
                playerTileY - topOffsetInTiles - 1, widthInTiles + 3,
                heightInTiles + 3);

        // draw entities relative to the player that must appear in the center
        // of the screen

        g.translate(400 - (int) (playerX * 32), 300 - (int) (playerY * 32));

        // draw other entities here if there were any

        g.resetTransform();

        if (isIntroOver == false) {

            int mx = container.getInput().getMouseX();
            int my = container.getInput().getMouseY();
            g.drawAnimation(circleAnim, mx - 32, my - 32);
            g.drawImage(titleImage, x, y);

            g.setColor(Color.black);
            g.fillRect(0, 450, 800, 150);

            g.drawImage(baseBGImage, fx, fy);
            g.drawAnimation(brenAnim, rx, ry);
            g.drawImage(basePotionImage, px - 50, py + 70);
            g.setColor(Color.white);
            g.drawRect(0, 450, 799, 149);

        }

        if (isIntroOver == true){

            g.drawImage(baseBGImage, fx, fy);
            //g.drawAnimation(brenAnim, rx, ry);
            g.drawImage(basePotionImage, px - 50, py + 70);
            //g.setColor(Color.black);
            //g.fillRect(0, 450, 800, 150);
            g.drawAnimation(brenAnim, 300, ry);
            g.drawRect(0, 450, 799, 149);
            g.drawRect(1, 451, 798, 148);

            //long slideTime = System.currentTimeMillis() + 2000;

            if (isWizardDrawn == true){

                g.translate(400 - (int) (playerX * 32), 300 - (int) (playerY * 32));

                if(wizList == null ){

                    System.out.println("true");
                }

                }

            }

            if (fy < 1000 && py < 900 ){
            fy = fy + 1;
            py = py + 2;

            //if (slideCount <=1){
            //slideTime = System.currentTimeMillis() + 2000;
            //slideCount++;}
            //else{
            //g.setColor(Color.black);
            //g.fillRect(0, 450, 800, 150);}

            }

            if(container.getInput().isKeyPressed(Input.KEY_ESCAPE)){

                isIntroOver = false;

        }}

    /**
     * 
     * 
     * @param argv
     *            The argument passed on the command line (if any)
     */
    public static void main(String[] argv) {
        try {
            // create a new container for our example game. This container
            // just creates a normal native window for rendering OpenGL
            // accelerated
            // elements to
            AppGameContainer container = new AppGameContainer(
                    new MainGameLoop(), 800, 600, false);
            container.setAlwaysRender(true);
            container.setVSync(true);
            container.setShowFPS(false);
            container.setMouseGrabbed(false);
            container.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
uidvcgyl

uidvcgyl1#

你的 init 方法初始化局部变量:

ArrayList<WizardObject> wizList = new ArrayList <WizardObject>(100);

类成员保持未初始化状态,因此它包含null(默认值):

public List<WizardObject> wizList;

改变

ArrayList<WizardObject> wizList = new ArrayList <WizardObject>(100);

wizList = new ArrayList <WizardObject>(100);
k4emjkb1

k4emjkb12#

问题是这一行创建了一个局部变量,使成员相形见绌 wizList :

ArrayList<WizardObject> wizList = new ArrayList <WizardObject>(100);

更改为:

wizList = new ArrayList <WizardObject>(100);

相关问题