You want counter.length() not counter.length()-1, when you create the array. The indexes of the array goes to n-1, not the length of the string (which is n).
When you loop thru the array, you want to start at count-1, and go down from there.
If the number is 12345, then your array needs to be of size 5, to hold the 5 digits, and the indexes go from 0-4. Accordingly, you want to create the array with count, and start printing at count-1.
That should address your index out of bounds issue, but you have other issues too.
Note your loop will stop as soon as it finds a zero, even if its in the middle of the digits. So if you have 3405, your app will print 5 and then stop.
Also, the comments to your question hint at overall simpler approaches, although what you are doing is fine for learning.