Help : Java Runtime Error , But Not in C++

Problem : https://www.codechef.com/problems/RICHSTR

i have written same code in java and c++ , but getting runtime error in java but not in c++
why ?? please, help me.

c++ : https://www.codechef.com/viewsolution/26031873
java : https://www.codechef.com/viewsolution/26031776