网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

怎样避免NullPointerExceptions(原创)

http://www.diybl.com/ 2007-2-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

写JAVA程序,NullPointerExceptions会经常陪伴着你
因为JAVA中到处都是对象,而每一个对象都可能为Null
怎样能减少这种错误呢?
我觉得应该尽量调用那些已经确定被实例话的对象的方法
比如
private String name = "";  // 给它初始化一个值不让它为null

public boolean isNameEqual(String paraName) {
    return name.equals(paraName);
  }
就比下面这种要好
  public boolean isNameEqual(String paraName) {
    return newName.equals(name);  // 这样如果paraName为空的话就会抛出一个 NullPointerException }

当然,如果在程序中name可能被赋值为null的话
那就要这样写了
public boolean isNameEqual(String paraName) {
    if (name != null) {
      return name.equals(paraName);
    }
    else if (paraName != null) {  // name 为 null 而paraName 不是
      return false;
    }
    else {  // 都为空
return true;  // 根据你的需要,返回false也行
    }
  }

至于
public class Animator extends java.applet.Applet implements Runnable{
Vector images;
int imgNumber;
int currentImage=1;
Thread thisThread;
public void init(){
imgNumber=new Integer(getParameter("imgNumber")).intValue();
for (int x=0;x<imgNumber;x++)
{Image img=getImage(getDocumentBase(),"img"+(x+1)+".jpg");
images.addElement(img);
}
}

我觉得可以先给imgNumber=0;
然后还要判断
getParameter("imgNumber")是否为null
这样更好些

文章整理:DIY部落 http://www.diybl.com (本站)   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言