Skip to content Skip to sidebar Skip to footer

NullPointerException AsyncTask Android

I have a problem with AsyncTask in android. I have a NullPointerException at this line ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CON

Solution 1:

The problem is with the Context object. It's null.

In android the context is very important to know which page is being considered to refresh with the corresponding changes.

When you call this method:

checkRete(Context context)

make sure that context is not null.


Solution 2:

The error is in line number 36 for your function class , you are using any object which is not initialized. OR else put some more code

Caused by: java.lang.NullPointerException
            at it.polimi.mobilecourse.expenses.Functions.checkRete(Functions.java:36)
            at it.polimi.mobilecourse.expenses.Functions.query(Functions.java:27)

Solution 3:

in addition to GingerHead's answer

First make sure your using the right context.

Create class member:

 Context mContext;

Initialize in onCreate of Activity:

  mContext = this;

Or if it's from a fragment in onActivityCreated call:

  mContext = getActivity();

Then use your mContext:

   Functions.query(mContext, "www.myurl.com");

Additionally make sure it's not null in your method before passing it to checkRete:

 static ArrayList<ObjDb> query(Context context, String url) {
    System.out.println(url);
    // check context before using it
    if(context != null){
        if(!checkRete(context)) {
            System.out.println("Problema rete");
            return null;
        }
    }
}

UPDATE

You are using onAttach(Activity activity) incorrectly, this is designed to attach interface callbacks that should be implemented in the parent for Fragment to Activity communication:

REMOVE this:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = (LandingActivity) activity;
}

then in:

 public void ftpControl(String url) {

    // Call getActivity() here
    new RequestFtp().setParameters(getActivity(), url, "controlloFB", LandingFragment.this).execute();
 }

Post a Comment for "NullPointerException AsyncTask Android"