...
| Code Block |
|---|
/**
* Interleave two strings.
* Assumes input parameters aren't null.
*/
def interleave(String a, String b) {
int maxlength = [a.size(), b.size()].max()
def sb = new StringBuffer()
(0..<maxlength).each{
if (it < a.size()) sb << a[it]
if (it < b.size()) sb << b[it]
}
sb
}
println interleave('Hello', 'World!')
// => HWeolrllod!
//println interleave(null, 'World!')
// => NullPointerException (somewhere within the method call)
|
If we call it with valid parameters, everything is fine. If we call it with null we will receive a NullPointerException during the method's execution. This can sometimes be difficult to track down.
...