I though using toArray() function will help. If you have any example of how to use toArray function without getting any error,it would be great help.
I think the issue here is that we don't know how you even got your code to compile -- much less get an NPE.
If you show us the code that generates the NPE (as the code you've shown us can't), we can help you figure out what is wrong.
Henry