1

My app is based off of GPS location and Google Maps. The problem I am having is when I try to run a Search By Zip code I get a NullPointerException error in the logcat.

Here is my HomeFragment.java code

@Override
    public void onClick(View v) {
        switch (v.getId()){
        case R.id.town_search:
            String town = (String) spinner.getSelectedItem().toString();
            agencySearch(town);
            mListener.onFragmentButton();
            break;
        case R.id.zip_search:
            Editable strZip  = ((EditText) v.findViewById(R.id.txt_zip)).getText();
            int zip = Integer.parseInt(strZip.toString());
            agencySearch(zip);
            mListener.onFragmentButton();
            break;
            case R.id.btn_location:
                 Geocoder geocoder;
                 String bestProvider;
                 List<Address> user = null;
                 double lat = 0;
                 double lng = 0;
                 int radius = 5;

                LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

                 Criteria criteria = new Criteria();
                 bestProvider = lm.getBestProvider(criteria, false);
                 Location location = lm.getLastKnownLocation(bestProvider);

                 if (location == null){
                     Toast.makeText(getActivity(),"Location Not found",Toast.LENGTH_LONG).show();
                  }else{
                    geocoder = new Geocoder(getActivity());
                    try {
                        user = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                    lat=(double)user.get(0).getLatitude();
                    lng=(double)user.get(0).getLongitude();
                    System.out.println(" DDD lat: " +lat+",  longitude: "+lng);

                    }catch (Exception e) {
                            e.printStackTrace();
                    }
                }
            agencySearch(lat, lng, radius);
            mListener.onFragmentButton();
            break;
        }
    }

The error I am receiving in logcat.

11-09 15:24:25.087: W/dalvikvm(10145): threadid=1: thread exiting with uncaught exception (group=0x417a5898)
11-09 15:24:25.087: E/AndroidRuntime(10145): FATAL EXCEPTION: main
11-09 15:24:25.087: E/AndroidRuntime(10145): java.lang.NullPointerException
11-09 15:24:25.087: E/AndroidRuntime(10145):    at com.wny.wecare.fragment.HomeFragment.onClick(HomeFragment.java:128)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at android.view.View.performClick(View.java:4475)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at android.view.View$PerformClick.run(View.java:18796)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at android.os.Handler.handleCallback(Handler.java:730)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at android.os.Looper.loop(Looper.java:137)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at android.app.ActivityThread.main(ActivityThread.java:5455)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at java.lang.reflect.Method.invokeNative(Native Method)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at java.lang.reflect.Method.invoke(Method.java:525)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-09 15:24:25.087: E/AndroidRuntime(10145):    at dalvik.system.NativeStart.main(Native Method)
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Nov 09 '14 at 23:07
  • just a little bit of difference but thanks. Its a What, How question and not a where question – Scott DeCota Nov 09 '14 at 23:35

0 Answers0