0

Hi i am new to android studio and I am trying to start a new activity - however, I am having endless issues with getting Context - I have tried a few different methods posted on stack overflow but It just keeps throwing a null pointer please help. See onCreate method and exception below.

AddRoutine.class is just a blank activity

MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity.mContext = this.getApplicationContext();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //Add Routine
        FloatingActionButton fab = findViewById(R.id.addRoutine);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, AddRoutine.class));
            }
        });

        generateRoutineListing(getAppContext());
        //if returnListing.length > 0
        // recyclerView.addItems
        //else
        // Show Jumbotron/Message board explaining that no routines have been created
    }

Exception

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: za.co.freelanceweb.routines, PID: 14648
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ComponentName.<init>(ComponentName.java:130)
        at android.content.Intent.<init>(Intent.java:5780)
        at za.co.freelanceweb.routines.MainActivity$1.onClick(MainActivity.java:38)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24774)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6518)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Thanks so Much

2 Answers2

0

Instead of:

 MainActivity.mContext = this.getApplicationContext();

Use:

Context mcontext = MainActivity.this;  //Also, set mcontext as a global variable.

Also,

Instead of:

 generateRoutineListing(getAppContext());

Use:

 generateRoutineListing(mcontext);
Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
0

The problem is probably in this line:

   MainActivity.mContext = this.getApplicationContext();

Activity extends Context so you can always use this to refer to the activity's context.

I am not sure what you are trying to do with that but you should not be using the application's context on one activity. The application context lives through out the entire lifetime of the application.

If you want want to start a new activity do this.

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, AddRoutineActivity.class);
        startActivity(intent);
}

And then register AddRoutineActivity in your AndroidManifest.xml file like so:

        <activity android:name=".AddRoutineActivity" />

If you are new to android you may want to check out Kotlin.

Gilbert
  • 2,699
  • 28
  • 29
  • Thanks for the response - I need the new activity to start onClick of the FAB button, the line you are referring to MainActivity.mContext = this.getApplicationContext(); is left from other attempts at getting the context - see following code ``` startActivity(new Intent(MainActivity.this, AddRoutine.class)); ``` – Stephen Venter Apr 22 '20 at 10:13
  • So you have found what you are looking for? – Gilbert Apr 22 '20 at 10:14
  • No - how do i get context in startActivity(new Intent(MainActivity.this, AddRoutine.class)); as it is always null even though i am specifying MainActivity.this – Stephen Venter Apr 22 '20 at 10:16
  • The context of the activity you are in is never null. For example, when you are in MainActivity the context is MainActivity so it is not null because the activity is running. Inside the click listener, `this` refers to the `view` the listener is setup on. To access the context of MainActivity you use `MainActivity.this`. – Gilbert Apr 22 '20 at 10:20
  • Comment out ` MainActivity.mContext = this.getApplicationContext();` and ` generateRoutineListing(getAppContext());` and lets see what you get – Gilbert Apr 22 '20 at 10:23
  • If i comment those lines out i get the same error - 'java.lang.String android.content.Context.getPackageName()' on a null object reference - so the issue has to do with line 38 which is where i pass the context to startActivity() in the onCreate – Stephen Venter Apr 22 '20 at 12:06
  • Whats the name of the file – Gilbert Apr 22 '20 at 12:09
  • Hey, did you solve your problem? If you didn't you may want to post the source code for the entire file so I can see if the problem is somewhere else. – Gilbert Apr 22 '20 at 17:15