Java - NullPointerException,更改数组值

时间:2014-01-23 01:21:17

标签: java arrays nullpointerexception

我正在做一个大学项目来制作俄罗斯方块,但是我现在卡住了。 当我只想更改数组的值时,我有一个nullpointerexception。应该很容易吗? 显然不是......

我还不是一名高级程序员,所以如果有人能帮我找到错误的代码,并特别向我解释为什么它错了,我们将不胜感激。

这是我的代码:

package tetris;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import engine.*;

public class Tetris extends Game {
    private int x = 0, y = 0, maxWidth = 0, bSize = 25;
    private int[][] field;
    private boolean busy;
    private Blok blok = null;

    public Tetris() {
        title = "Tetris";
        height = 600;
        width = 400;
        delay = 100;
        delay2 = 10;
        field = new int[20][10];
        field[19][7] = 1;
        field[18][7] = 2;
        field[17][7] = 3;
        field[19][2] = 4;
        field[19][3] = 5;
    }

    public static void main(String arg[]) {
        GameApplication.start(new Tetris());
    }

    @Override
    /**
     * Right Arrow: keycode 39
     * Left Arrow: keycode 37
     */
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == 37) {
            if (tryMove(-1)) {
                x--;
            }
        } 
        else if(e.getKeyCode() == 39) {
            if (tryMove(1)) {
                x++;
            }
        } else if(e.getKeyCode() == 40) {
            if (tryMove(0)) {
                y++;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void update() {
        if(!busy) {
            int random = (int) (Math.random()*7);
            //debug mode
            random = 0;
            ///
            System.out.println(random);
            blok = new Blok(random);
            y = 1;
            x = 3;
            busy = true;
        }
        if (!tryMove(0)){
            field[y][x] = 1;
            busy = false;
        } else {
            y++;
        }
        scanLine();
    }

    @Override
    public void update2() {
    }

    @Override
    public void draw(Graphics2D g) {
        g.fillRect(0, 0, width, height);
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, 250, 500);

        for (int i = 0; i<20; i++){
            for (int j = 0; j<10; j++){
                if (field[i][j]>0) {
                    g.setColor(Color.black);
                    g.fillRect(j*25,i*25,bSize,bSize);
                }   
            }
        }

        if(busy) {
            maxWidth = blok.getWidth();
            for (int i = 0; i<3; i++){
                g.fillRect(blok.getX(i)*25,blok.getY(i)*25,bSize,bSize);
            }
        }
    }

    public boolean tryMove(int a) {
        boolean b = true;
        if (a == -1){
            if (x <= 0 || field[y][x-1] != 0){
                b = false;
            }
        } else if (a == 0){
            if (y >= 19 || field[y+1][x] != 0){
                b = false;
            }

        } else if (a == 1){
            if (x >= (9-maxWidth) || field[y][x+1] != 0){
                b = false;
            }
        } 
        return b;
    }

    public void scanLine(){
        for (int i = 0; i<20; i++){
            boolean vol = true;
            for (int j = 0; j<10; j++){
                if (field[i][j]==0) {
                    vol = false;
                }
            }
            if (vol) {
                removeLine(i);
            }
        }
    }
    public void removeLine(int a){
        for (int i = a; i>0; i--){
            for (int j = 0; j<10; j++){
                field[i][j] = field[i-1][j];
            }
        }
    }
}

用于制作块(以及出错的地方)

package tetris;

public class Blok {
    private int type;
    private int[] x,y;
    private int width;

    public Blok(int type) {
        this.setType(type);
        setCoords(type);
        x = new int[4];
        y = new int[4];
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setCoords(int type) {
        if (type == 0){
            setWidth(3);
            setX(0,1); setY(0,0);
            setX(1,2); setY(1,0);
            setX(2,3); setY(2,0);
            setX(3,0); setY(3,0);
            //x[0] = 1; y[0] = 0;
            //x[1] = 2; y[1] = 0;
            //x[2] = 3; y[2] = 0;
            //x[3] = 0; y[3] = 0;
        } else if (type == 1){
            setWidth(2);
            x[0] = 1; y[0] = 0;
            x[1] = 2; y[1] = 0;
            x[2] = 0; y[2] = -1;
            x[3] = 0; y[3] = 0;
        } else if (type == 2){
            setWidth(2);
            x[0] = 1; y[0] = 0;
            x[1] = 2; y[1] = 0;
            x[2] = 2; y[2] = -1;
            x[3] = 0; y[3] = 0;
        } else if (type == 3){
            setWidth(2);
            x[0] = 1; y[0] = 0;
            x[1] = 2; y[1] = 0;
            x[2] = 1; y[2] = -1;
            x[3] = 0; y[3] = 0;
        } else if (type == 4){
            setWidth(2);
            x[0] = 1; y[0] = -1;
            x[1] = 1; y[1] = 0;
            x[2] = 0; y[2] = -1;
            x[3] = 0; y[3] = 0;
        } else if (type == 5){
            setWidth(1);
            x[0] = 1; y[0] = 0;
            x[1] = 1; y[1] = -1;
            x[2] = 2; y[2] = -1;
            x[3] = 0; y[3] = 0;
        } else if (type == 6){
            setWidth(2);
            x[0] = 1; y[0] = 0;
            x[1] = 1; y[1] = 1;
            x[2] = 2; y[2] = 1;
            x[3] = 0; y[3] = 0;
        }
    }

    public int getX(int type) {
        return x[type];
    }

    public int getY(int type) {
        return y[type];
    }

    public void setX(int index, int value){
        x[index] = value;  //This is line 82, goes wrong here.
    }

    public void setY(int index, int value){
        y[index] = value;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int w) {
        this.width = w;
    }

}

这是错误代码:

Exception in thread "Thread-2" java.lang.NullPointerException
    at tetris.Blok.setX(Blok.java:82)
    at tetris.Blok.setCoords(Blok.java:26)
    at tetris.Blok.<init>(Blok.java:10)
    at tetris.Tetris.update(Tetris.java:75)
    at engine.Engine.run(Engine.java:18)

1 个答案:

答案 0 :(得分:3)

在构造函数中放入

 public Blok(int type) {
        this.setType(type);
        setCoords(type);
        x = new int[4];
        y = new int[4];
    }

您拨打setCoords拨打setX(..)setX使用x null,然后NullPointerException被投放。

解决方法是更改​​订单。

   public Blok(int type) {
            this.setType(type);
            x = new int[4];
            y = new int[4];
            setCoords(type);            
   }

但是在构造函数中调用可覆盖的方法并不是一个好习惯。阅读更多What's wrong with overridable method calls in constructors?。解决方法是使setCoords(..)成为最终版或使您的类成为最终版或使该方法private或从构造函数中删除,并从此类的客户端代码中调用它。