Reputation: 170
I'm having some strange errors reports on Google Play Developer Console regarding NPEs on Activity context or Application context. I can't recreate this error on my device and I know it's working fine on most devices.
This is my Main Activity, the one that is first opened on app startup:
public class ActSplash extends Activity {
private Sync sync;
private Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_splash);
do {
ctx = this.getApplicationContext(); //Using just "this" doesn't work either.
} while (ctx==null);
sync = Sync.getInstance(ctx); //Apparently ctx is null at this point in some devices
}
This is my Sync class (Singleton)
public class Sync {
private Context ctx;
private static Sync INSTANCE = null;
public static Sync getInstance(Context ctx) {
if (INSTANCE == null) createInstance(ctx);
return INSTANCE;
}
private synchronized static void createInstance(Context ctx) {
if (INSTANCE == null) {
INSTANCE = new Sync(ctx);
}
}
private Sync(Context _ctx)
{
ctx = _ctx;
//NPE on following line
WifiManager wifiMan = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
...
}
This is the stack trace:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.xxx.xxx/com.xxx.xxx.ActSplash}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.xxx.xxx.Sync.<init>(SourceFile:82)
at com.xxx.xxx.Sync.void createInstance(android.content.Context)(SourceFile:67)
at com.xxx.xxx.Sync.com.xxx.xxx.Sync getInstance(android.content.Context)(SourceFile:72)
at com.xxx.xxx.ActSplash.void onCreate(android.os.Bundle)(SourceFile:40)
at android.app.Activity.performCreate(Activity.java:5058)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2104)
... 11 more
What can I do to make sure I have ctx!=null
before calling:
sync = Util_Sync.getInstance(ctx);
Upvotes: 0
Views: 3319
Reputation: 3544
Would you mind posting the full constructor ? null pointer exception occurs somewhere inside of Sync()
constructor. _ctx
is not null though
Upvotes: 0
Reputation: 44834
so as you have code that does not change
do {
ctx = this.getApplicationContext(); //Using just "this" doesn't work either.
} while (ctx==null);
this is going to result in an endless loop, if it fails to get ctx
.
Activity is a context! See http://developer.android.com/reference/android/content/Context.html
Known Indirect Subclasses AbstractInputMethodService, AccessibilityService, AccountAuthenticatorActivity, ActionBarActivity, Activity, ActivityGroup, AliasActivity, Application, BackupAgent, BackupAgentHelper, ContextThemeWrapper, and 23 others.
Try
ctx = this;
Update
Further to your Sync code being posted
private Context ctx;
is never used, so remove it.
Upvotes: 1
Reputation: 83527
Note that this
can never be null
. Assume to the contrary that it can be, then this means you would have a call such as myObj.myMethod()
where myObj
is null
. But then this method call would throw a NPE before execution enters myMethod()
.
This means that your error is coming from somewhere else.
Upvotes: 0