-2

My code on getting current location is working when I'm using map fragment, but when I use it in another class without displaying the map, I'm getting nullpointerException in String provider. Can you please check what's wrong with my program and if possible explain why I'm getting this error.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = inflater
            .inflate(R.layout.layout_school_list, container, false);
    lvList = (ListView) v.findViewById(R.id.lvSchools);
    LocationManager locationManager = (LocationManager) getSystemService("location");
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    final Location currentLoc = locationManager
            .getLastKnownLocation(provider);
    if (currentLoc != null) {
        onLocationChanged(currentLoc);
    }

    if (Distance==true){
        double lat = currentLoc.getLatitude();
        double lng = currentLoc.getLongitude();
        ArrayList<Double> rangeList = getRangeList(lat, lng);
        ArrayList<String> theList1 = new ArrayList<String>();
        for (int count = 0; count < rangeList.size(); count++)
        {
            String temp = sTuition[count];
            theTuitionFee = Integer.parseInt(temp);
            for(int subloop=0; subloop<sSpecialty[count].length; subloop++) {
            List<String> values = (List<String>)thisFilter[4];
            if (rangeList.get(count) <= DistanceVal
                    &&sReg[count].equalsIgnoreCase((String)thisFilter[0])
                    && sAdmin[count].equalsIgnoreCase((String)thisFilter[1])
                    && sAmbience[count].equalsIgnoreCase((String)thisFilter[3])
                    && amountTF >= theTuitionFee
                    && filtered(sSpecialty[count][subloop], values)) {
                theList1.add(sList[count]);

            }
            }
        }

        displayNearbyList(theList1);
    }

Here's my methods:

public void onLocationChanged(Location location) {
    double currentLat = location.getLatitude();
    double currentLng = location.getLongitude();

}

And also getting error with getSystemService and my IDE requires me to put this

 private LocationManager getSystemService(String string) {
    // TODO Auto-generated method stub
    return null;
}

which is not used in my other working class.

Here's my stack trace . I'm new here , so I don't know how to post stack trace properly

08-29 12:14:45.017: E/AndroidRuntime(6864): FATAL EXCEPTION: main 08-29 12:14:45.017: E/AndroidRuntime(6864): java.lang.NullPointerException 08-29 12:14:45.017: E/AndroidRuntime(6864): at capstone.app.recommender.SchoolListFragmentFilter.onCreateView(SchoolListFragmentFilter.java:67) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.os.Handler.handleCallback(Handler.java:725) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.os.Handler.dispatchMessage(Handler.java:92) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.os.Looper.loop(Looper.java:137) 08-29 12:14:45.017: E/AndroidRuntime(6864): at android.app.ActivityThread.main(ActivityThread.java:5041) 08-29 12:14:45.017: E/AndroidRuntime(6864): at java.lang.reflect.Method.invokeNative(Native Method) 08-29 12:14:45.017: E/AndroidRuntime(6864): at java.lang.reflect.Method.invoke(Method.java:511) 08-29 12:14:45.017: E/AndroidRuntime(6864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-29 12:14:45.017: E/AndroidRuntime(6864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-29 12:14:45.017: E/AndroidRuntime(6864): at dalvik.system.NativeStart.main(Native Method)

  • Please provide your stack trace – Zarwan Aug 29 '15 at 04:33
  • What is the error you are getting, post logs. Post the Code where its not working. – Pankaj Aug 29 '15 at 04:37
  • I already mention it , here's where i'm getting the error . String provider = locationManager.getBestProvider(criteria, true); I'm new here so I dont know how to add stack trace sorry . wait I'll try it – carl john santos Aug 29 '15 at 04:40

2 Answers2

1

You should replace your line

LocationManager locationManager = (LocationManager) getSystemService("location");

With this one

LocationManager locationManager = (LocationManager)<your context>.getSystemService(Context.LOCATION_SERVICE);
EEJ
  • 678
  • 5
  • 12
0
 private LocationManager getSystemService(String string) {
    // TODO Auto-generated method stub
    return null;
}

This is the method you have to imlpement by yourself, as you return null. the locationManager below is null, and therefore throws NullPointerException

String provider = locationManager.getBestProvider(criteria, true);

check the post below to know how should you implement getSystemService()

How does getSystemService() work exactly?

Community
  • 1
  • 1
Derek Fung
  • 8,171
  • 1
  • 25
  • 28