Java - NullPointerException,更改数组值

标签 java arrays nullpointerexception

我正在做一个制作俄罗斯方 block 的大学项目,但是我现在陷入困境。 当我只想更改数组的值时,出现空指针异常。应该很容易吧? 显然不是...

我还不是高级程序员,所以如果有人能帮助我找到不正确的代码,并向我具体解释为什么它是错误的,我将不胜感激。

这是我的代码:

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];
            }
        }
    }
}

以及制作 block (以及出错的地方)

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)

最佳答案

在你的构造函数中放入

 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(..) 设置为 Final 或将您的类设置为 Final 或将该方法设置为 private 或从构造函数中删除并从此类的客户端代码中调用它。

关于Java - NullPointerException,更改数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21297520/

相关文章:

java - 比较两个 ArrayList

ios - 如何在 swift 3 中将日期范围转换为字符串?

arrays - 尝试打印数组的一个元素以某种方式打印所有元素

java - Java 数组上线程 "main"java.lang.NullPointerException 中的异常

java - 在java中解析1GB xml数据的最佳解析器

java - 在这种情况下如何动态为 ResultSet 提供列名称?

java - JDK 环境变量(Ubuntu 12.04)与 tomcat

java - Java中数组的类是什么

java - android中出现NullPointer异常如何解决?

java - JDBC – create 语句中出现空指针异常