空指针异常

时间:2011-06-07 19:28:04

标签: blackberry

我正在构建一个应用程序但是在运行我的代码时,我一直在调试它时得到一个nullpointer异常我似乎没有注意到它可能会给它一个错误,我得到它在涂层线上,我需要把它这个方法的类有更好的解释吗? 感谢

protected void paint(Graphics graphics) { 
     graphics.setColor(Color.BLACK); 
     graphics.fillRect(0, 0, getWidth(), getHeight());
  // graphics.drawBitmap(xInt, yInt, imgWidth, imgHeight, _currentPicture, 0, 0);
}

2 个答案:

答案 0 :(得分:1)

我能看到的唯一可能导致NullPointerException的行就是_currentPicturenull。您可以通过将代码更改为:

来测试理论
protected void paint(Graphics graphics) {
    if(_currentPicture != null) {
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics
            .drawBitMap(xInt, yInt, imgWidth, imgHeight, _currentPicture, 0, 0);
    }
}

答案 1 :(得分:0)

变量_currentPicture未初始化。在代码中查看更远的内容并查看其设置位置。没有更多的代码或完整的堆栈跟踪,我们无法做更多的事情。