Question

My aplication class is activity to get aplication context and use it else where.isOnline is called from some class JSONParser, which is not an activity. isOnline is called by getJSONFromUrl(). Test is method of B class.test() is called by activity LoginActivity. However i am getting nullpoint and dont understand why. is it because i am gtting context in wrong way or i pass context to ConnectivityManager incorecly or something else? method isOnline

private static Boolean isOnline(){

        context = MyApplication.getAppContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
         if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
               for (int i = 0; i < info.length; i++) {
                  if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                     return true;
                  }
               }
            }
         }
         return false;
    }

myaplication class

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

error

06-05 09:29:22.104: D/dalvikvm(526): GC_EXTERNAL_ALLOC freed 1033 objects / 82800 bytes in 52ms
06-05 09:29:52.734: D/frakc@mail.ru(526): emails
06-05 09:29:56.414: D/f(526): onItemSelected

06-05 09:30:00.446: W/KeyCharacterMap(526): No keyboard for id 0
06-05 09:30:00.446: W/KeyCharacterMap(526): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-05 09:30:01.814: D/parsing data to login(526): true
06-05 09:30:01.824: W/dalvikvm(526): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
06-05 09:30:01.824: E/AndroidRuntime(526): FATAL EXCEPTION: Thread-10
06-05 09:30:01.824: E/AndroidRuntime(526): java.lang.NullPointerException
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.adapter.JSONParser.isOnline(JSONParser.java:87)
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.adapter.JSONParser.getJSONFromUrl(JSONParser.java:114)
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.UserFunctions.loginUser(UserFunctions.java:51)
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.LoginActivity$1$1.run(LoginActivity.java:144)
06-05 09:30:01.824: E/AndroidRuntime(526):  at java.lang.Thread.run(Thread.java:1096)
Was it helpful?

Solution

your context is coming null. pass the application context in your method and then use that context for the refere.. it will work fine

OTHER TIPS

Change:

MyApplication.context = getApplicationContext();

to

MyApplication.context = this;

Do the following things if you haven't done yet :

1. Add you application class in your Manifest in your applications name like this

 <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:name=".MyApplication"

2. Add ACCESS_NETWORK_STATE permission

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top