Jon
Jon

Reputation: 121

Android Null Pointer Exception with Intent/IntentService

My application crashes returning the following stack trace:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.**REDACTED**.copyright/com.example.**REDACTED**.copyright.MainActivity}: java.lang.NullPointerException
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.os.Looper.loop(Looper.java:136)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.reflect.Method.invoke(Method.java:515)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at dalvik.system.NativeStart.main(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933): Caused by: java.lang.NullPointerException
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.content.ComponentName.<init>(ComponentName.java:77)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.content.Intent.<init>(Intent.java:3834)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at com.example.**REDACTED**.copyright.MainActivity.<init>(MainActivity.java:31)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.Class.newInstanceImpl(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.Class.newInstance(Class.java:1208)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2119)

From what I can tell the error occurs in line 31 of my MainActivity class line 31 and a few of the surrounding lines are shown below:

public class MainActivity extends Activity {
Intent mServiceIntent = new Intent(this, Scan.class);

mServiceIntent is not reference again until the the lines shown below which are also in MainActivity:

 @Override
protected void onStop() {
    super.onStop();

    this.startService(mServiceIntent);
    Log.v("MainActivity", "onStop called");

If there is anymore information you need I will be happy to add it. Thank you in advance for your patience and assistance.

Upvotes: 0

Views: 178

Answers (2)

Selim Raza
Selim Raza

Reputation: 497

Use this code hope this will worked well

Intent intent = new Intent(this, SplashScreen.class);                          
 intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent );

Upvotes: 0

Al Wang
Al Wang

Reputation: 354

You should instantiate the intent in your onStop() method (which happens during runtime) as opposed to defining it in compile time. E.g.:

public class MainActivity extends Activity {
    Intent mServiceIntent;
...
@Override
protected void onStop() {
    super.onStop();
    mServiceIntent = new Intent(this, Scan.class);
    this.startService(mServiceIntent);
    Log.v("MainActivity", "onStop called");

Upvotes: 2

Related Questions