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 = "rbproeditor@gmail.com";
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!

tccpg288
  • 3,242
  • 5
  • 35
  • 80
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Krease Dec 15 '15 at 16:20
  • I do not believe this is a duplicate as this example relates specifically to calling an item from SharedPrefs. – tccpg288 Jan 27 '16 at 03:19

1 Answers1

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)){ 
...
lganzzzo
  • 558
  • 3
  • 10