This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Shyam, A stack overflow usually means an infinite loop or code that calls itself. Once in a while, it means you are just using too much memory. It's good practice to rule out the common causes before looking at the memory. And you aren't doing anything memory intensive.
Try simplifying the code to see if the error still occurs. For example, what happens if you comment out the loop with index "k". If the problem still occurs, you can comment out more code. If not, you know where to examine more closely.
When you post java code and want us to look at it, we'll all thank you if you enclose the code in [ c o d e ] [ / c o d e ] tags (without the spaces). This will preserve the indents in your code and make it infinitely easier to read.