-1

I have a class, which has a method that uses the connectivity manager. This method from this class is called by a fragment to check connectivity. But when I do this, Null pointer exception occurs.

I have tried using getActivity() as context for connection manager, but it didnt help at all. Here is the code:
Class file:

public class Common_Tasks implements AsyncResponse {
int InternetConnectionStatus=-1;
static int MemberShipStatus=-1;

Context ctx;
Common_Tasks(Context context)
{
    this.ctx = context;
}

public void ShowMessage(String msg)
{
    Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show();
}


public int CheckInternetConnectivity(){
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if(networkInfo!=null && networkInfo.isConnected()){
        InternetConnectionStatus=1;
    }
    else {
        InternetConnectionStatus=0;
    }
    return InternetConnectionStatus;
}

public void StartAsyncActivity()
{
    BackgroundTask backgroundTask = new BackgroundTask();
    backgroundTask.delegate = this;
    backgroundTask.execute();
}

@Override
public void processFinish(String output) {
    if(output.equals("true"))
        MemberShipStatus=1;
    else if(output.equals("false"))
        MemberShipStatus=0;
}
}

Fragment:

public class NoInternetConnection extends Fragment {
Common_Tasks commonTasks = new Common_Tasks(getActivity());

public NoInternetConnection() {

}


@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_nointernet, container, false);

    final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_id);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);
            Handler handler = new Handler(Looper.getMainLooper());
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                    if(commonTasks.CheckInternetConnectivity()==1)
                        commonTasks.ShowMessage("Internet connection restored!");
                    else
                        commonTasks.ShowMessage("No Internet connection");
                }
            };
            handler.postDelayed(r, 1500);
        }
    });

    return view;
}

}

Error Log:

    09-15 17:09:18.209 16400-16400/com.example.tonymathew.eliteenglishclub E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.example.tonymathew.eliteenglishclub, PID: 16400
                                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                                             at com.example.tonymathew.eliteenglishclub.Common_Tasks.CheckInternetConnectivity(Common_Tasks.java:25)
                                                                                             at com.example.tonymathew.eliteenglishclub.NoInternetConnection$1$1.run(NoInternetConnection.java:37)
                                                                                             at android.os.Handler.handleCallback(Handler.java:746)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-15 17:14:11.634 19927-19927/com.example.tonymathew.eliteenglishclub E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.example.tonymathew.eliteenglishclub, PID: 19927
                                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                                             at com.example.tonymathew.eliteenglishclub.Common_Tasks.CheckInternetConnectivity(Common_Tasks.java:25)
                                                                                             at com.example.tonymathew.eliteenglishclub.NoInternetConnection$1$1.run(NoInternetConnection.java:37)
                                                                                             at android.os.Handler.handleCallback(Handler.java:746)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68

2 Answers2

0

You have to respect the android lifecycle. This is a common issue for android beginners. You can not put this here

public class NoInternetConnection extends Fragment {
Common_Tasks commonTasks = new Common_Tasks(getActivity());

Since the Fragment at this point is not attached to the Activity. Put that into the onCreate() method of your Fragment. For future references see the android lifecycle

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0
    public class NoInternetConnection extends Fragment {
    Common_Tasks commonTasks;

    public NoInternetConnection() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nointernet, container, false);
        commonTasks = new Common_Tasks(getActivity());

}
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41