XML : Null Pointer Exception in app purchase? [duplicate]

This question already has an answer here:

  **Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getBuyIntent(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)` on a null object reference**    

I am trying to make an android application with a simple button that says buy. When the user clicks on it, I want to charge them money. Here is the code:

  public class MainActivity extends AppCompatActivity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            Intent serviceIntent =                  new Intent("com.android.vending.billing.InAppBillingService.BIND");          serviceIntent.setPackage("com.android.vending");          bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);      }        IInAppBillingService mService;        ServiceConnection mServiceConn = new ServiceConnection() {          @Override          public void onServiceDisconnected(ComponentName name) {              mService = null;          }            @Override          public void onServiceConnected(ComponentName name,                                         IBinder service) {              mService = IInAppBillingService.Stub.asInterface(service);          }      };        @Override      public void onDestroy() {          super.onDestroy();          if (mService != null) {              unbindService(mServiceConn);          }      }        public void buy(View v) {          ArrayList<String> skuList = new ArrayList<String>();          skuList.add("premiumUpgrade");          Bundle querySkus = new Bundle();          querySkus.putStringArrayList("ITEM_ID_LIST", skuList);              Bundle buyIntentBundle = null;          try {              buyIntentBundle = mService.getBuyIntent(3, getPackageName(),                      "premiumUpgrade", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");          } catch (RemoteException e) {              e.printStackTrace();          }          PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");            try {              startIntentSenderForResult(pendingIntent.getIntentSender(),                      1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),                      Integer.valueOf(0));          } catch (IntentSender.SendIntentException e) {              e.printStackTrace();          }        }        @Override      protected void onActivityResult(int requestCode, int resultCode, Intent data) {          if (requestCode == 1001) {              int responseCode = data.getIntExtra("RESPONSE_CODE", 0);              String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");              String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");                if (resultCode == RESULT_OK) {                  try {                      JSONObject jo = new JSONObject(purchaseData);                      String sku = jo.getString("productId");                      Toast.makeText(MainActivity.this, "" +sku, Toast.LENGTH_SHORT).show();                  }                  catch (JSONException e) {                      Toast.makeText(MainActivity.this, "" +e.toString(), Toast.LENGTH_SHORT).show();                      e.printStackTrace();                  }              }          }}  }    

However, When I do this, I get:

  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getBuyIntent(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference    

However, it should not be null, because it is getting initialized in onServiceConnected, right?

Thanks,

Ruchir

No comments:

Post a Comment