4

I am making an Android app that captures a user's e-mail through the displayEmailCaptureFragment() method. If I already captured the user's e-mail, I do not want to prompt the user to enter their e-mail again.

I declare the customerEmailAddress String right after my MainActivity but before the onCreate method:

public class MainActivity extends FragmentActivity implements View.OnClickListener, BillingProcessor.IBillingHandler,
    EmailCapturedListener {

private MyPagerAdapter pageAdapter;
private Button mDownloadResumeButton;
private ImageButton mRightArrow, mLeftArrow;
private static final String EMAILKEY = "email_key";
public static final String EDITSKU = "professional_editing_service_1499";
public static final String EMAILSKU = "resume_template_pro_99";
private static final String RBPEMAIL = "[email protected]";
private static final String RBPPASSWORD = "Redhawks123";
public String customerEmailAddress;

I then have an OnClick() method based on a user's response action within the app. Essentially, I am try to allow for a certain activity after the onClick if the user already entered their e-mail address. If they did not enter their e-mail address, then I prompt them to enter their e-mail and save it to shared preferences. I use a basic if - else statement, however I am still generating a null pointer exception even though I assign the customerEmailAddress string:

@Override
public void onClick(View v) {

    int currentPosition = pager.getCurrentItem();
    switch (v.getId()) {
        case R.id.right_arrow:
            pager.setCurrentItem(currentPosition + 1);
            break;
        case R.id.left_arrow:
            pager.setCurrentItem(currentPosition - 1);
            break;
        case R.id.download_resume_button:
                customerEmailAddress = mPrefs.getString(EMAILKEY, null);
            if (customerEmailAddress.equals(null)){
                displayEmailCaptureFragment();
            }
            else {
                showPurchaseDialog();
        }
            break;
    }
}

Any help is appreciated!

2
  • Possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Dec 15, 2015 at 16:20
  • I do not believe this is a duplicate as this example relates specifically to calling an item from SharedPrefs. Commented Jan 27, 2016 at 3:19

1 Answer 1

4

This code is wrong - you can't call equals(...) or any other method on a null object.

...
customerEmailAddress = mPrefs.getString(EMAILKEY, null);
if (customerEmailAddress.equals(null)){
...

Do like this instead:

customerEmailAddress = mPrefs.getString(EMAILKEY, null);
if (customerEmailAddress == null){ 
...

OR use TextUtils:

customerEmailAddress = mPrefs.getString(EMAILKEY, null);
if (TextUtils.isEmpty(customerEmailAddress)){ 
...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I will modify accordingly and let you know if I have questions.
You are welcome! If you done with it - mark as resolved. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.