xforum 的编码规范规定:必须对输入的参数进行 null 验证用的是 validation 里的一个方法,检查对象是否为 null :
public static void validatenotnull( final object testobject )
{
// if object is null, then an exception is thrown
if ( testobject == null )
{
throw new illegalargumentexception( “object cant be null.” );
}
}
如果把它改造成下面的形式,会使输出更加明显:
public static void validatenotnull(string objectname, object object) {
if ( object == null ) {
throw new illegalargumentexception( objectname + ” cant be null !!!” );
}
}
比如在真正的程序里:
public void checklogon( string username, string password ) {
validation.validatenotnull( “username”, username );
validation.validatenotnull( “password”, password );
// …
}
以后,在程序运行的过程中,如果再出现 username 为 null 的时候程序就会输出:java.lang.illegalargumentexception: username cant be null !!!
哈哈,再不用为找 null 犯愁了。养成好的习惯,预防错误的发生,可以节省将来的好多时间。
