Its a very known problem. There are couple of issues behind the scene and you need to solve them in order to make the code working.
First, is your device connected with internet? If not, give you device some time to extract your location info from GPS.Or, connect your device with internet.
Second, is your device's gps working properly? Is it ON?
Third, check the condition if the code can extract user location from his device. Debug this line
if(gps.cangetlocation())
carefully. If all are ok, this will return a TRUE values and you are ready to go.
Fourth, GPS needs sometime to get user location depending on your location depth. So wait patiently, sometimes device restart solves the problem too.