我有一个空指针异常错误

时间:2016-07-05 00:52:51

标签: java nullpointerexception keylistener

这是一段代码:

public class Keyboard implements KeyListener {

    private Boolean[] keys = new Boolean[120];
    public Boolean up, down, left, right;

    public void update(){
        up = keys[KeyEvent.VK_UP];
        down = keys[KeyEvent.VK_DOWN];
        left = keys[KeyEvent.VK_LEFT];
        right = keys[KeyEvent.VK_RIGHT];

        for (int i = 0; i < keys.length; i++) {
            if(keys[i]){
                System.out.println(i);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent ke) {
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        keys[ke.getKeyCode()] = true;
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        keys[ke.getKeyCode()] = false;
    }

}

我有空指针异常是for循环,我把条件检查是否真实?

0 个答案:

没有答案