1

I'm trying to implement PayPal functionality in an app. In order to get it working, I'm trying to create a simple app that only shows the PayPal button that, once pressed, will pass all of the necessary parameters to the library to take the user through the log-in and payment process.

When I run this code, I get the following error message:

03-11 20:48:05.588 23566-23566/org.kevinbright.android.paypaldemo E/AndroidRuntime: FATAL EXCEPTION: main Process: org.kevinbright.android.paypaldemo, PID: 23566 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.kevinbright.android.paypaldemo/org.kevinbright.android.paypaldemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference

The error relates to this section:

((RelativeLayout) findViewById(R.id.RelativeLayout01)).
            addView(launchPayPalButton);

Here's the code:

 package org.kevinbright.android.paypaldemo;

public class MainActivity extends Activity   {

    private CheckoutButton launchPayPalButton;
    final static public int PAYPAL_BUTTON_ID = 10001;
    private static final int REQUEST_PAYPAL_CHECKOUT = 2;
    private double _theSubtotal;
    private double _taxAmount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initLibrary();
        showPayPalButton();

    }

    private void showPayPalButton() {
        //LinearLayout linearLayout = new LinearLayout(this);
        //linearLayout.setOrientation(LinearLayout.VERTICAL);

        //ViewGroup.LayoutParams linearLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        //setContentView(linearLayout, linearLayoutParam);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.bottomMargin = 10;

        // Generate the PayPal checkout button and save it for later use
        PayPal pp = PayPal.getInstance();
        launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY);

        // The OnClick listener for the checkout button
        launchPayPalButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                payPalButtonClick(arg0);
            }
        });

        // Add the listener to the layout
        launchPayPalButton.setLayoutParams(params);
        launchPayPalButton.setId(PAYPAL_BUTTON_ID);
        ((RelativeLayout) findViewById(R.id.RelativeLayout01)).
                addView(launchPayPalButton);

    }

    public void payPalButtonClick(View arg0) {
        PayPalPayment newPayment = new PayPalPayment();
        newPayment.setSubtotal(new BigDecimal(_theSubtotal));
        newPayment.setCurrencyType("USD");
        newPayment.setRecipient("my@email.com");
        newPayment.setMerchantName("My Company");
        Intent checkoutIntent = PayPal.getInstance().checkout(newPayment, this);
        startActivityForResult(checkoutIntent, REQUEST_PAYPAL_CHECKOUT);


    }



    public void initLibrary() {
        PayPal pp = PayPal.getInstance();

        if (pp == null) {  // Test to see if the library is already initialized

            // This main initialization call takes your Context, AppID, and target server
            pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", PayPal.ENV_NONE);

            // Required settings:

            // Set the language for the library
            pp.setLanguage("en_US");

            // Some Optional settings:

            // Sets who pays any transaction fees. Possible values are:
            // FEEPAYER_SENDER, FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, and FEEPAYER_SECONDARYONLY
            pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);

            // true = transaction requires shipping
            pp.setShippingEnabled(false);
        }

    }

}

I don't understand why I'm getting this error message, since launchPayPalButton references pp, which is an object of the PayPal Class that is initialized in the initLibrary method before showPayPalButton is called.

Please help!!!

Edit: This is not a duplicate of the questions referenced. I have reviewed that post and that post is about referening objects that have not been initialized and thus, return a null reference. This question explains why this should not generate a null pointer exception because the object is initialized.

Edit 2: Here's my .xml file called "customize.xml" that saved in the Layouts folder under "Res."

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"
                android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
                >
</RelativeLayout>
Kevin Bright
  • 961
  • 2
  • 9
  • 17
  • Yes, I've reviewed that post, and I've explained why I don't believe (or understand why) it is a null pointer exception. – Kevin Bright Mar 12 '16 at 02:15
  • `(RelativeLayout) findViewById(R.id.RelativeLayout01)` is returning null. Are you sure `R.id.RelativeLayout01` is the id of an accessible view when it is being called? – M.S. Mar 12 '16 at 02:17
  • 1
    he never set the view to Activity, so obviously any findViewById call will return null – Selvin Mar 12 '16 at 02:18
  • 1
    How to debug a null pointer is asked dozens of times per day. You need to learn how to debug one on your own, its pretty much debugging 101. Now if you find a function returning null that you think shouldn't be, or a variable becoming null when it shouldn't, then you may have a question. Until then these questions are all going to be closed as dupes, you aren't doing your share of the research. – Gabe Sechan Mar 12 '16 at 02:24
  • The problem was not anything to do with 'launchPayPalButton'. Instead, the problem was with 'addView' since I forget to set the view via 'setContentView' in 'onCreare'. – Kevin Bright Mar 12 '16 at 22:52

0 Answers0