0

I have a class (DataSource.java) containing a method, which is calling another method in my MainActivity, which receives a context and a string variable.

In the Datasource.java, when calling the method from the MainActivity, I have set the context variable to null, and even if I set it to "context" I receive the same error as I launch the app: EXCEPTION error for this line: context = getApplicationContext();.

Could someone suggest me, where am I wrong?

DataSource.java:

public class ToursDataSource {

 public boolean addToFollowList(Tour tour) {
    ContentValues values = new ContentValues();
    values.put(ToursDBOpenHelper.COLUMN_IMAGE, tour.getTitle());                
    long result = database.insert(ToursDBOpenHelper.TABLE_FOLLOW, null, values);

    MainActivity orgName = new MainActivity();
    orgName.sendRegId(this, "Test string");

    return null;        
 }
}

MainActivity:

Context context;
    ...

        public void sendRegId(Context context, String organization) {

                 //PROBLEM IS AT THIS LINE
                context = getApplicationContext();

                // Check device for Play Services APK. If check succeeds, proceed with
                //  GCM registration.
                if (checkPlayServices()) {
                    gcm = GoogleCloudMessaging.getInstance(this);
                    regid = getRegistrationId(context);
                    Log.i(TAG, "Registration started!");
                    Log.d("RegisterActivity", "GCM RegId is: " + regid);           

                    if (regid.isEmpty()) {

                      registerInBackground();
                    }
                } else {
                    Log.i(TAG, "No valid Google Play Services APK found.");
                }

                Sender results = new Sender();
                results.shareRegIdWithAppServer(regid, organization);


            }


        ...

Logcat:

04-16 15:42:07.765: E/AndroidRuntime(15610): FATAL EXCEPTION: main
04-16 15:42:07.765: E/AndroidRuntime(15610): java.lang.NullPointerException
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.MainActivity.sendRegId(MainActivity.java:189)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.ToursDataSource.addToFollowList(ToursDataSource.java:205)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.TourDetailActivity.onOptionsItemSelected(TourDetailActivity.java:129)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.view.View.performClick(View.java:4475)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.view.View$PerformClick.run(View.java:18786)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Handler.handleCallback(Handler.java:730)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Looper.loop(Looper.java:137)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.app.ActivityThread.main(ActivityThread.java:5493)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at java.lang.reflect.Method.invoke(Method.java:525)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at dalvik.system.NativeStart.main(Native Method)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89

3 Answers3

0

Make change like this:

context = getApplicationContext();

to

this.context = context;
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
  • 1
    Voting does not obligate to comment, whether it's up- or down-voting. This is according to community guidelines. Even though it may be frustrating to not know the reason for a vote. – Manuel Jan 02 '19 at 15:58
0

I guess MainActivity is a Activity class. You cannot instantiate a Activity class. You declare Activities in manifest file. Each Activity has a lifecycle. You cannot treat Activity class a normal java class.

To know the reason Read raghav sood's answer @

Can i Create the object of a activity in other class?

Quoting Raghav's Sood

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @Achilles so what is that you want to point out. And the reason for downvote? That is null coz you can't create an instance of activity class – Raghunandan Apr 16 '14 at 13:30
  • thanks @Raghunandan, I have to move thing int an "Utility class", because as you have said MainActivity cannot be treated as a normal class. – Edmond Tamas Apr 16 '14 at 13:32
  • @EdmondTamas yes you can't do `MainActivity orgName = new MainActivity();` and the reason is well mentioned in the link – Raghunandan Apr 16 '14 at 13:33
  • @EdmondTamas you choose static context??. Note: Holding reference to activity context longer than the life cycle of the activity leads to memory leaks – Raghunandan Aug 05 '15 at 15:57
0

try to use this:

static Context = mConext;

i think this works.

Ashish Tikarye
  • 850
  • 8
  • 11