Fix for Null Pointer Exception

arr[arr.length]!=0 should be arr[arr.length-1]!=0 to avoid a null pointer exception. 

The if statement is short-circuiting on arr[0]!=0 most of the time hiding this bug.