Devrath
Devrath

Reputation: 42824

nullpointer exception while using location class object in android fragments

What is happening

I am getting nullpointer exception in line:

initialNetLocation = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);

This is not happening in case of Activity !

My Code:

public class MyClass extends Fragment {

    //Constructor
    public MyClass () {
    }

    //View references
    ImageView mImageView;
    EditText mEditText;
    double latitude,longitude;
    LocationManager locationManager;
    private AppLocationService appLocationService;
    private Location locationPassive,initialNetLocation;

    String finalLastDate,finalLastTime,finalCurDate,finalCurTime;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.findurgentcare, container,false);

        mImageView=(ImageView) rootView.findViewById(R.id.iv_get_adress_from_location);
        mEditText=(EditText) rootView.findViewById(R.id.et_adress_of_loc);

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();

        mImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                initialNetLocation =appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
   }
}

Upvotes: 0

Views: 51

Answers (2)

Tushski
Tushski

Reputation: 242

You didn't initialize appLocationService before using it, therefore you are receiving NullPointer Exception:

appLocationService = new AppLocationService(<CONTEXT>);

Upvotes: 0

Jens
Jens

Reputation: 69450

appLocationService is never initialized.

Upvotes: 2

Related Questions